[SOLVED] NPE When adding location to config

Discussion in 'Spigot Plugin Development' started by MCAeolus, May 4, 2015.

  1. So, when using (Bukkit 1.8, grabbed with the rev arg in BuildTools) to develop a plugin, I came upon an error that Im not sure about. I get an NullPointerException when trying to add to a config. Here is the snippet(s) of code:

    Code (Text):
    } else if (args[0].equals("setcrate")) {

                    if (p.getTargetBlock((Set<Material>) null, 50).getType() == Material.CHEST) {

                        Block b = p.getTargetBlock((Set<Material>) null, 50);

                        configm.addBlock(b, p);

                        return true;

                    }
                } else if (args[0].equals("delcrate")) {

                    if (p.getTargetBlock((Set<Material>) null, 50).getType() == Material.CHEST) {

                        Block b = p.getTargetBlock((Set<Material>) null, 50);

                        configm.removeBlock(b, p);
                        return true;

                    }

                } else {

                    p.sendMessage(Crates.light_red + "[CratesGUI] Invalid argument: " + args[0].toString() + ".");
                    return true;

                }
    &

    Code (Text):
     public static Crates plugin;

        public void addBlock(Block b, Player p){

            String blockdata = b.getWorld().getName() + ", " + b.getX() +
                    ", " + b.getY() + ", " + b.getZ();
            if(!plugin.blockSaveC.contains("crates")){
                plugin.blockSaveC.options().copyDefaults(true);


            }else {
                if (!plugin.blockSaveC.contains("crates." + blockdata)) {
                    plugin.blockSaveC.set("crates." + blockdata, true);

                    plugin.saveBlockData();

                    p.sendMessage(Crates.light_green + "[CratesGUI] This is now a crate.");
                } else {
                    p.sendMessage(Crates.light_red + "[CratesGUI] This is alredy a crate! Use /crategui delcrate to remove.");
                }
            }
        }

        public void removeBlock(Block b, Player p){

            String blockinfo = b.getWorld().getName() + ", " + b.getX() +
                    ", " + b.getY() + ", " + b.getZ();
            if(!plugin.blockSaveC.contains("crates")) {
                plugin.blockSaveC.options().copyDefaults(true);

            }else{
                if (plugin.blockSaveC.contains("crates." + blockinfo)) {

                    plugin.blockSaveC.set("crates." + blockinfo, null);

                    plugin.saveBlockData();

                    p.sendMessage(Crates.light_green + "[CratesGUI] This is no longer a crate!");

                    return;

                } else {

                    p.sendMessage(Crates.light_red + "[CratesGUI] This is not a crate!");
                    return;

                }
            }

        }
    Thanks!
     
  2. Inkzzz

    Resource Staff

    You're saving it as null....

    Code (Text):
     plugin.blockSaveC.set("crates." + blockinfo, null);
     
    • Funny Funny x 2
  3. @MCAeolus stop abusing static for access (if you don't know any other way to access objects, learn more Java).

    Also, your plugin variable wasn't initialised, create a constructor which takes a plugin instance and assigns it to the field.
    that will just remove the path from the config.
     
  4. @DarkSeraphim
    *snip*

    Woops, now I understand. XD Sorry, was glancing at this in between doing other things. Let me work on the plugin some, and Ill get back to the post with my outcomes.
     
    #4 MCAeolus, May 4, 2015
    Last edited: May 5, 2015
  5. So from yesterday, I have done some modifications (no longer accessing objects outside of class via static, etc.) and am still getting a NPE. This errors on line ' configm.addBlock(b, p); ' (In command class). I tried adding checks for the variables, etc. , and added a try catch around the method, but it still recieves a NPE. I believe I am getting the other class (ConfigManagement) fine, and nothing shows up as incorrect in IntelliJ when I access ConfigManagement via a private constructor in my command class. Any idea what could be causing the NPE?

    @DarkSeraphim
    (Or anyone else)
     
  6. Hmm... so
    Code (Text):
        ConfigManagement configm = new ConfigManagement();
     
    would be null?
     
    #7 MCAeolus, May 5, 2015
    Last edited: May 5, 2015
  7. @MCAeolus post the stack trace and your latest code. I bet the error is not exactly on that line.
     
  8. Alright,
    @DarkSeraphim
    StackTrace when running command /cg setcrate:
    Code (Text):
    [07:58:43] [Server thread/INFO]: MCAeolus issued server command: /cg setcrate
    [07:58:43] [Server thread/WARN]: java.lang.NullPointerException
    [07:58:43] [Server thread/WARN]:     at com.mcaeolus.crates.ConfigManagement.addBlock(ConfigManagement.java:22)
    [07:58:43] [Server thread/WARN]:     at com.mcaeolus.crates.CrateGUICommand.onCommand(CrateGUICommand.java:37)
    [07:58:43] [Server thread/WARN]:     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [07:58:43] [Server thread/WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [07:58:43] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13)
    [07:58:43] [Server thread/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [07:58:43] [Server thread/WARN]:     at java.util.concurrent.FutureTask.run(Unknown Source)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634)
    [07:58:43] [Server thread/WARN]:     at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537)
    [07:58:43] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
    Code for Command Class:
    Code (Text):
    ConfigManagement configm = new ConfigManagement();

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

            if (sender instanceof Player) {

                Player p = (Player) sender;

                if (args.length == 0) {

                    p.sendMessage(Crates.yellow + "[ChestGUI] " + Crates.white + "Developed by MCAeolus with <3 for OmegaFactions.");
                    return true;

                } else if (args[0].equals("setcrate")) {

                    if (p.getTargetBlock((Set<Material>) null, 50).getType() == Material.CHEST) {

                        Block b = p.getTargetBlock((Set<Material>) null, 50);

                        if (p.isOnline()) {

                            try {

                                configm.addBlock(b, p);

                            } catch (NullPointerException e) {

                                e.printStackTrace();

                            }
                        }

                        return true;

                    }else{

                    p.sendMessage(Crates.light_red + "[CratesGUI] Targeted block can not be a crate!");
                    return true;

                }
                } else if (args[0].equals("delcrate")) {

                    if (p.getTargetBlock((Set<Material>) null, 50).getType() == Material.CHEST) {

                        Block b = p.getTargetBlock((Set<Material>) null, 50);

                        try {

                            configm.removeBlock(b, p);

                        }catch (NullPointerException e){

                            e.printStackTrace();

                        }

                        return true;

                    }else{

                        p.sendMessage(Crates.light_red + "[CratesGUI] Targeted block is not a crate!");
                        return true;

                    }

                } else {

                    p.sendMessage(Crates.light_red + "[CratesGUI] Invalid argument: " + args[0].toString() + ".");
                    return true;

                }
            } else {

                System.out.println("[ChestGUI] Console cannot use this command argument!");
                return true;
            }

        }

    }
    And code for Config class:

    Code (Text):
        private Crates plugin;
        public ConfigManagement(Crates plugin){
            this.plugin = plugin;
        }

        public void addBlock(Block b, Player p){

            String blockdata = b.getWorld().getName() + ", " + b.getX() +
                    ", " + b.getY() + ", " + b.getZ();

            if(!plugin.blockSaveC.contains("crates")){

                plugin.blockSaveC.options().copyDefaults(true);

            }else {
                if (!plugin.blockSaveC.contains("crates." + blockdata)) {
                    plugin.blockSaveC.set("crates." + blockdata, true);

                    plugin.saveBlockData();

                    p.sendMessage(Crates.light_green + "[CratesGUI] This is now a crate.");
                } else {
                    p.sendMessage(Crates.light_red + "[CratesGUI] This is already a crate! Use /crategui delcrate to remove.");
                }
            }
        }

        public void removeBlock(Block b, Player p){

            String blockinfo = b.getWorld().getName() + ", " + b.getX() +
                    ", " + b.getY() + ", " + b.getZ();
            if(!plugin.blockSaveC.contains("crates")) {
                plugin.blockSaveC.options().copyDefaults(true);

            }else{
                if (plugin.blockSaveC.contains("crates." + blockinfo)) {

                    plugin.blockSaveC.set("crates." + blockinfo, null);

                    plugin.saveBlockData();

                    p.sendMessage(Crates.light_green + "[CratesGUI] This is no longer a crate!");

                    return;

                } else {

                    p.sendMessage(Crates.light_red + "[CratesGUI] This is not a crate!");
                    return;

                }
            }

        }
     
    EDIT: Lines in stacktrace are:
    Code (Text):
    //CommandClass
    configm.addBlock(b, p);

    &

    //ConfigClass
    if(!plugin.blockSaveC.contains("crates")){
     
     
    #9 MCAeolus, May 5, 2015
    Last edited: May 5, 2015
  9. @MCAeolus in the ConfigManagement, plugin is null. You added a no args constructor, which obviously doesn't set the plugin field (as you have no plugin instance). Remove that no args constructor, and use the constructor that takes a plugin instance as parameter.

    You probably need to move the initialisation to the constructor of the command class, which will also require a plugin instance as variable so you have an instance to pass to the config class's constructor.
     
  10. @DarkSeraphim I want to thank you for all your help! (And for standing my noobishness).
    I actually modified some of this (Combined my ConfigClass into my CommandClass, and modified the plugin variable) and now have everything working. Thanks so much!