Solved NPE in GMHook Class.

Discussion in 'Spigot Plugin Development' started by Cloaking_Ocean, May 15, 2016.

  1. Hello. I've been trying to make a plugin that will exchange diamonds for a certain rank. I'm using the class GMHook. When I run a command: /bribedranks bribe <donator> i run these line of code,
    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("bribedranks")) {
                if (args.length == 0) {
                    sendHelpMessage(sender);
                    return true;
                }else{
                    //Commands:
                    /*
                     * bribedranks - shows help page
                     *   - bribe - shows arguments: /bribedranks bribe <rank>
                     *     - <rank> - Checks if they have the correct items. If they do, removes items and sets group
                     *   - ranks - shows available ranks
                     *  
                     */
                 
                    if (args[0].equalsIgnoreCase("bribe")) {
                        if (args.length == 1) {
                            sender.sendMessage(ChatColor.GOLD + "Usage: /bribedranks bribe <rank>");
                            return true;
                        }
                        if (sender instanceof Player) {
                            Player player = (Player) sender;
                            String rank = args[1].toUpperCase();
                         
                            if (getConfig().contains(rank.toLowerCase())) {
                                int bribe = getConfig().getInt(rank.toLowerCase());
                             
                                if (player.getInventory().getItemInHand().getType() == Material.DIAMOND) {
                                    if (player.getInventory().getItemInHand().getAmount() >= bribe) {
    //                                    try {
                                            if (gm.setGroup(player, rank)) {
                                                ItemStack stack = player.getItemInHand();
                                                ItemMeta meta = stack.getItemMeta();
                                                ItemStack newStack = new ItemStack(stack.getType(), stack.getAmount()-bribe);
                                                newStack.setItemMeta(meta);
                                                player.getInventory().setItemInHand(newStack);
                                                player.sendMessage(ChatColor.GREEN + "Successfully set rank to " + rank + "!");
                                                return true;
                                            }else{
                                                player.sendMessage(ChatColor.RED + "You have no handler " + rank + ".");
                                                return true;
                                            }
    //                                    }catch (NullPointerException e) {
    //                                        player.sendMessage(ChatColor.RED + "There is no rank named " + rank + ".");
    //                                        return true;
    //                                    }
                                    }
                                }else{
                                    player.sendMessage(ChatColor.RED + "You must have " + bribe + " diamond(s) to bribe this rank.");
                                    return true;
                                }
                            }else{
                                player.sendMessage(ChatColor.DARK_RED + "You cannot bribe this rank");
                                return true;
                            }
                        }else{
                            notPlayer(sender);
                            return true;
                        }
                     
                    }else if(args[0].equalsIgnoreCase("ranks")) {
                        for (String s : getConfig().getKeys(false)) {
                            sender.sendMessage(s.substring(0, 1).toUpperCase() + s.substring(1));
                        }
                        return true;
                    }
                }
            }
            return true;

    Once this code has run I get an error on this line: "if (gm.setGroup(player, rank)) {"
    The stack trace is this:

    Code (Text):
    [12:40:29] [Server thread/INFO]: Cloaking_Ocean issued server command: /bribedranks bribe DONATOR
    [12:40:29] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'bribedranks' in plugin BribedRanks v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
        at com.exitium.bribedranks.GMHook.setGroup(GMHook.java:63) ~[?:?]
        at com.exitium.bribedranks.Main.onCommand(Main.java:60) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.jar:git-Spigot-bf0ac55-3fc97ff]
        ... 14 more

    Code (Text):

    public boolean setGroup(final Player base, final String group)
        {
            final OverloadedWorldHolder handler = groupManager.getWorldsHolder().getWorldData(base);
            if (handler == null)
            {
                return false;
            }
            handler.getUser(base.getName()).setGroup(handler.getGroup(group));
            return true;
        }
    The line "final OverloadedWorldHolder handler = groupManager.getWorldsHolder().getWorldData(base);" causes the error. I'm not sure why. I'm casting the Player object from the CommandSender object. How can I fix this error? Thanks in advance.
     
  2. Something is null. Either you put breakpoints, or you use System.out.println() to figure out which item it is. I bet you forgot instantiate groupManager
     
  3. @Ferdz

    So I considered your suggestion and found that the groupManager object was indeed null I reviewed the method in which it gets instantiated:

    Code (Text):
    @EventHandler(priority = EventPriority.MONITOR)
        public void onPluginEnable(final PluginEnableEvent event)
        {
            final PluginManager pluginManager = plugin.getServer().getPluginManager();
            final Plugin GMplugin = pluginManager.getPlugin("GroupManager");
            if (GMplugin != null && GMplugin.isEnabled())
            {
                groupManager = (GroupManager)GMplugin;
            }
        }
    I tested whether this function is actually getting called at all and it seems not to be called at all. Now I'm wondering if i'm suppose to manually call this method either in the constructor of the GMHook class or in my OnEnable in my main class. If I were to do so, what would I pass in for the PluginEnableEvent object?

    Also I should note what my current onEnable method looks like:
    Code (Text):
    GMHook gm = null;
       
        @Override
        public void onEnable() {
            gm = new GMHook(this);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    Thank you for your help so far!
     
    #3 Cloaking_Ocean, May 17, 2016
    Last edited: May 17, 2016
  4. Even though using onPluginEnable is not the most efficient method, it should work. However, you forgot to register the event listener in your onEnable
     
  5. @Ferdz Oh right. I completely forgot xD. Thanks for the help.