My command manager class is throwing null pointer exception, why?

Discussion in 'Plugin Development' started by Benyon, Sep 7, 2019.

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

    Benyon

    So I'v done a lot of plugin developing, but never tackled commands for some reason. This is my code

    Code:
    public class Main extends JavaPlugin {
        private static Main plugin;
        private CommandManager commandMgr;
    
        @Override
        public void onEnable() {
            // Declaring variables
            plugin = this;
    
            // Configuration version handler
            ConfigManager.loadConfig();
    
            if (!getDescription().getVersion().equals(this.getConfig().getString("version"))) {
                plugin.saveResource("config.yml", true);
                getLogger().info("Config version does not match plugin version, overwriting config, please ensure you adjust appropriately...");
                getLogger().info("Plugin version: [" + getDescription().getVersion() + "] Config version: [" + getConfig().getString("version") + "]");
            }
    
            // Create recipes required for plugin
            Utilities.createRecipes();
    
            // Start reading events of the server
            registerEvents();
    
            // Start Cooldown mechanisms
            Cooldowns.CooldownTimer();
    
            commandMgr = new CommandManager(this);
            getCommand("raidaccept").setExecutor(commandMgr);
     
        }
    }
    And this is the CommandManager class

    Code:
    public class CommandManager implements CommandExecutor {
    
        private final Main plugin;
        public CommandManager(Main para) {
            this.plugin = para;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("raidaccept")) {
                sender.sendMessage(ChatColor.GOLD + "Raid accepted");
                for (String var : args) {
                    sender.sendMessage(ChatColor.GOLD + "String Args: " + var);
                }
                return true;
            }
            return false;
        }
    }
    
    The console shows this

    [07-09-2019 21:28:50] java.lang.NullPointerException: null

    On line 45, which is.

    Code:
            commandMgr = new CommandManager(this); 
    In the main class.

    is 'this' null? and why would it be?

    I was missing it out of plugins.yml

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 7, 2019
  2. Offline

    KarimAKL

    @Benyon Unless you register multiple commands in the same class, you won't need to check if the command name is "raidaccept".
    'this' can never be null; it would be impossible to call a method from something that is null. :p
    Does that mean that this is solved?
     
Thread Status:
Not open for further replies.

Share This Page