Solved error onEnable

Discussion in 'Plugin Development' started by Badwolf330, Oct 4, 2015.

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

    Badwolf330

    error:
    Code:
    22:57:18] [Server thread/ERROR]: Error occurred while enabling Efteling-Core v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at nl.eftelingcore.skyrob.main.onEnable(main.java:27) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot.jar:git-Spigot-1544]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [spigot.jar:git-Spigot-1544]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-1544]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot.jar:git-Spigot-1544]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot.jar:git-Spigot-1544]
        at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:360) [spigot.jar:git-Spigot-1544]
        at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:334) [spigot.jar:git-Spigot-1544]
        at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:290) [spigot.jar:git-Spigot-1544]
        at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:210) [spigot.jar:git-Spigot-1544]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [spigot.jar:git-Spigot-1544]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1544]
    
    source:
    Code:
    package nl.eftelingcore.skyrob;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        public static main plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
      
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is uitgezet!");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " is geactiveerd!");
            getCommand("efteling").setExecutor(plugin);
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
      
        }  
    
    
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
              
                return false;
        }
    }
    
    
    
    
    does someone knows a fix?
     
  2. Offline

    SuperSniper

    @Badwolf330
    Code:
    at nl.eftelingcore.skyrob.main.onEnable(main.java:27) ~[?:?]
    
    Please show us Line #27 of "main" class
     
  3. Offline

    Badwolf330

    that is the main class xD
     
  4. Offline

    RoboticPlayer

    Change your logger field and that will fix it.
    Code:
    //Change this
    public final Logger logger = Logger.getLogger("Minecraft")
    //To this
    Logger logger = this.getLogger();
    Then do this
    Code:
    //Change this
    this.logger.info(string);
    // To this
    logger.info(string);
    However, keep in mind that you don't need to print when the plugin is enabled & disabled, Bukkit already does this.
     
  5. Offline

    Protophite

  6. Offline

    SuperSniper

    @Badwolf330
    You edited in the main class, thanks :p

    For commands, you should make a separate class that's handling the commands on it's own.
    Here's how to do that:

    1. Make A Second Class That implements CommandExecutor

    Code:
    public class Command implements CommandExecutor {
    
    2. Do your command thing as normal.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
             
                return false;
        }
    }
    
    3. In your onEnable(), do this:

    Code:
    getCommand("COMMANDNAME").setExecutor(new Command());
    


    @henderry2019 That is something that he needs to do, but it's not the problem that he's worrying about. Changing the logger would just make his code more proficient, but the problem would be there still. The problem is that he's getting the command from his Main class, which is not implementing CommandExecutor, and it's not even checking the command name, so it's making his NPE.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
  7. Offline

    Badwolf330

    @SuperSniper
    Code:
    package nl.eftelingcore.skyrob;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        public static main plugin;
        public final Logger logger = this.getLogger();
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is uitgezet!");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " is geactiveerd!");
            getCommand("efteling").setExecutor(new Command());
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
       
        }   
    
    
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
               
                return false;
        }
    }
    
    Code:
    package nl.eftelingcore.skyrob;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class Command implements CommandExecutor {
    
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
             
                return false;
        }
    }
    
    like this???
     
  8. Offline

    SuperSniper

    @Badwolf330 Yes, but in the Command class, make sure to check for the command "efteling"
     
  9. Offline

    Badwolf330

    main
    Code:
    package nl.eftelingcore.skyrob;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        public static main plugin;
        public final Logger logger = this.getLogger();
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is uitgezet!");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " is geactiveerd!");
            getCommand("efteling").setExecutor(new Command());
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
       
        }   
    }
    
    
    
    
    @SuperSniper
    Code:
    java.lang.Error: Unresolved compilation problems: 
        The method setExecutor(CommandExecutor) in the type PluginCommand is not applicable for the arguments (Command)
        Cannot instantiate the type Command
    
        at nl.eftelingcore.skyrob.main.onEnable(main.java:27) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot.jar:git-Spigot-1544]
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
  10. Offline

    SuperSniper

    @Badwolf330 Oh, sorry about that :p Change your "Command" class name, because "Command" is a Bukkit import and is protected so it breaks the plugin
     
    Badwolf330 likes this.
  11. Offline

    Badwolf330

    :p thnx for your help #like
     
  12. Offline

    SuperSniper

    @Badwolf330 You're welcome :) please mark the thread as "Solved"
     
  13. Offline

    Badwolf330

    @SuperSniper
    still got a error:
    Code:
    Error occurred while enabling Efteling-Core v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at nl.eftelingcore.skyrob.main.onEnable(main.java:24) ~[?:?]
    
    main line 24
    Code:
    getCommand("efteling").setExecutor(new commandhandeler());
    
    commandhandeler is the name of the command class[/QUOTE]
     
  14. Offline

    RoboticPlayer

    Can you post your updated main class and command class?
     
  15. Offline

    SuperSniper

    @Badwolf330
     
  16. Offline

    Badwolf330

    Code:
    package nl.eftelingcore.skyrob;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        public static main plugin;
        public final Logger logger = this.getLogger();
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is uitgezet!");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " is geactiveerd!");
            getCommand("efteling").setExecutor(new commandhandeler());
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
       
        }   
    }
    
    command class
    Code:
    package nl.eftelingcore.skyrob;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class commandhandeler implements CommandExecutor {
    
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
             
                return false;
        }
    }
    
     
  17. Offline

    SuperSniper

    @Badwolf330 Check for the Command inside of your commandhandeler class.

    Code:
    
    package nl.eftelingcore.skyrob;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class commandhandeler implements CommandExecutor {
    
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                 if(cmd.getName().equalsIgnoreCase("efteling")) {
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
             
                return false;
        }
    }
    }
    
    
     
  18. Offline

    RoboticPlayer

    A) You don't need to log when the plugin is enabled/disabled, Bukkit already does this
    B) Due to A, you can remove your onDisable
    C) Why are you implementing listener and registering events if you don't have any events in the class?
    D) Java Naming Conventions: Class names should capitalize internal words (commandhandeler.java should be CommandHandeler.java)
    E) onCommand can be overridden
    F) Returning false will say the usage in the plugin.yml back to you

    Not necessary. If an onCommand method only is handling one command, you don't need to check the command.
     
  19. Offline

    SuperSniper

    @henderry2019 He can't remove his onEnable, he needs it to register the command.
     
  20. Offline

    RoboticPlayer

    I meant to say onDisable
     
  21. Offline

    Badwolf330

  22. Offline

    Scimiguy

  23. Offline

    SuperSniper

    @Scimiguy He has it registered inside the plugin.yml, if he didn't he wouldn't have this problem, he would have an Unknown Command problem.
     
  24. Offline

    Badwolf330

    @SuperSniper

    Whats the problem than xD I neer this for more things in my plugin
    Gr bart
     
  25. Offline

    boomboompower

  26. Offline

    Badwolf330

    My plugin yml file is fine,

    Code:
    commands:
      efteling:
        discription: Hey
    @SuperSniper
    How can I fix this error than??

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 5, 2015
  27. Offline

    timtower Administrator Administrator Moderator

  28. Offline

    Badwolf330

    I got the error
    Code:
    Error occurred while enabling Efteling-Core v1.0 (Is it up to date?
    java.lang.NullPointerException
    at nl.eftelingcore.skyrob.main.onEnable(main.java:24) ~[?:?]
    
    Main cass
    Code:
    package nl.eftelingcore.skyrob;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        public static main plugin;
        public final Logger logger = this.getLogger();
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is uitgezet!");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info(pdfFile.getName() + " Version: " + pdfFile.getVersion() + " is geactiveerd!");
            getCommand("efteling").setExecutor(new commandhandeler());
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
     
        } 
    }
    
    command class
    Code:
    package nl.eftelingcore.skyrob;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class commandhandeler implements CommandExecutor {
    
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){         if(cmd.getName().equalsIgnoreCase("efteling")) {   if(cmd.getName().equalsIgnoreCase("efteling")) {
          if (args.length == 0) {
                sender.sendMessage(ChatColor.GREEN + "hey");
            }
            if (args.length == 1)
                if (args[0].equalsIgnoreCase("test1")) {
                    sender.sendMessage(ChatColor.AQUA + "Status");
                }
                if (args[0].equalsIgnoreCase("test2")) {
                sender.sendMessage(ChatColor.AQUA + "omroep");
                }
           
                return false;
        }
    
    }
    }
    

    @SuperSniper
    @timtower
     
  29. Offline

    RoboticPlayer

    @Badwolf330 Discription should be spelled description. That may fix your issue.
     
  30. Offline

    Xerox262

    Oh my god, why are you teaching people incorrect standards, it's like you don't even know yourself. If he's using the command executor for a single command then he doesn't have to check it, if he's using it for multiple then he would
     
Thread Status:
Not open for further replies.

Share This Page