Solved NullPointerException

Discussion in 'Plugin Development' started by farget92, Jul 29, 2015.

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

    farget92

    Hello again,

    I've found some confusing error, and I don't know how to remove it.
    Because it doesn't make any sense.

    Code:
    public class CommandClass implements CommandInterface {
    
        private final Elements plugin;
        private CommandHandler commandHandler;
        private Permission permission;
       
        private Player player;
        private Command command;
        private String commandLabel;
        private String[] arg;
       
        public CommandClass(Elements plugin){
            this.plugin = plugin;
            this.commandHandler = plugin.getCommandHandler();
           
            this.permission = new Permission("");
           
        }
       
        @Override
        public void execute(CommandSender sender, Command command, String commandLabel, String[] arg) { 
           some stuff ....
           this.selfClass();
        }
    
        private void selfClass(){
           
            if(arg.length == 1){
                return;
            }
           
            switch(arg[1].toLowerCase()){
                case "armor":
                    return;
                case "skills":
                    return;
                default:
                    //commandHandler.badUsedForm(player, new String[]{"/el class, /el class armor, /el class skills"});
                    //There is error
                    player.sendMessage(plugin.toString());
                    player.sendMessage(commandHandler.toString());
                    return;
           
            }
    }
    It seems that commandHandler is null. But when I use:
    plugin.getCommandHandler().badForm(.....);
    It works good.

    Why constructor don't wont set commandHandler ?
    I've try this also with permission, but it works.

    Can you help me ?
     
  2. Offline

    Boomer

    you have a very confusing layout for your code, as well, you aren't clearly indicating what the nature of the problem is.

    But it is worth noting that arg is not set in your code, so the method that checks arg==1 will always be false , causing the default to execute. And it does not appear that you set the player object either, so would get errors calling player object references.
     
  3. Offline

    farget92

    @bwfcwalshy Thansk for advice :/ , I know how to setUp command handler or command.
    @Boomer Why is my layout confusing ? NullPointerException is not about the player object. Pleayer object exists. But on the second line.
    Object commandHandler is null. But I don't know why ? Because this object is get in constructor.
    And I know that plugin.getCommandHandler(); is not null.

    I'll find that error. It was caused by:
    Getting commandHandler instance before creating has been finished. Like this code was called by constructor of commandHandler. :D Of constructor :D
    But thanks for your effort.
     
    Last edited by a moderator: Jul 29, 2015
Thread Status:
Not open for further replies.

Share This Page