Help with creating a constructor for main class D:

Discussion in 'Plugin Development' started by adam561, Aug 17, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    adam561

    So I have 3 classes, Main, Commands, and Utils. My issue is that whenever I try to do anything with my main class that I call from via constructor, I get a NPE. This is my current utils constructor:
    Code:
    Main pl;
        public Utils(Main instance) {
            pl = instance;
        }
    My utils code is called by my commands class with the same constructor.

    This is what the NPE points at:
    Code:
    for (String path : pl.getConfig().getStringList("chests." + w.getName() + ".1")) {
    It isn't only for getting things from the config, whenever I try to do pl.getLogger().log(Whatever) or anything with the pl variable I get a NPE. I have narrowed it down to the pl variable being null.

    Full error:
    http://prntscr.com/4dt4sy


    I don't see the error with my constructor.

    HELP!!!!
     
  2. Can you post the full class(es)?
     
  3. Offline

    teej107

    Well you aren't assigning your "pl" object to anything obviously. You could be passing a null variable through the constructor. It's hard to give more input since you didn't post much code to work with.
     
  4. Offline

    adam561

    Command class:
    Code:
    public class Commands implements CommandExecutor{
     
        Main pl;
        public Commands(Main instance) {
            pl = instance;
        }
        Utils utils = new Utils(pl);
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("fill")){
                if(!(sender instanceof Player))return false;
                Player p = (Player) sender;
                pl.getServer().broadcastMessage("test");
                utils.startChests(p.getWorld());
            }
     
            return false;
        }
    }
    
    Main Class:
    Code:
    public class Main extends JavaPlugin{
        Commands cms = new Commands(this);
        Utils utils = new Utils(this);
     
        @Override
        public void onEnable() {
     
            final FileConfiguration config = this.getConfig();
            config.options().copyDefaults(true);
     
            getCommand("fill").setExecutor(cms);
     
     
        }
    Utils:
    Code:
    public class Utils {
     
        Main pl;
        public Utils(Main instance) {
            pl = instance;
        }
     
        public void startChests(World w) {
     
     
                for (String path : pl.getConfig().getStringList("chests." + w.getName() + ".1")) {
                    String[] cords = path.split(" ");
                    int x = Integer.parseInt(cords[0]);
                    int y = Integer.parseInt(cords[1]);
                    int z = Integer.parseInt(cords[2]);
     
     
                    Block block = w.getBlockAt(x, y, z);
                    if (!(block.getType().equals(Material.CHEST))) {
                        pl.getLogger().severe("Unable to locate chest at: " + x + " , " + y + " , " + z);
                        return;
                    }
            }
     
    }
     
  5. Offline

    Rocoty

    Because initialisation outside the constructor is called before the constructor. Therefore when you create the new Utils object in your Commands class with pl as argument, pl has not yet been initialised and you are passing null. Consider doing all field initialisation in the constructor to avoid such issues in the future.
     
  6. Offline

    adam561

    Thanks! I knew that It was something like that!
     
Thread Status:
Not open for further replies.

Share This Page