Solved How to fix this null pointer exception?

Discussion in 'Spigot Plugin Development' started by Champ_, Jul 10, 2018.

  1. I can't seem to figure out how to fix this null pointer. Help?

    Code (Text):
    Caused by: java.lang.NullPointerException
        at me.champ.hub_core.commands.NPCreate.onCommand(NPCreate.java:25) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-3d850ec-809c399]
        ... 15 more
    Code (Java):
    package me.champ.hub_core.commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.champ.hub_core.Core;

    public class NPCreate implements CommandExecutor {
       
        private Core core = Core.getInstance();

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (sender instanceof Player) {
                Player player = (Player) sender;
               
                if (cmd.getName().equalsIgnoreCase("npc")) {
                   
                    if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("create")) {
                            String npcname = args[1];
                            core.npcmanager.createNPC(player, npcname);
                            return true;
                        }
                    }
                }
            }
           
            return false;
        }
       
       

    }
     
  2. Your core instance or npcmanager in core is null.
     
    • Agree Agree x 1
  3. Code (Java):

    core.npcmanager.createNPC(player, npcname);
     
    This is throwing the NPE, take a better look at it. "npcmanager" might be null.
     
  4. Wow, such a silly mistake. Thanks guys! <3
     
    • Like Like x 1