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


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

    public class Main extends JavaPlugin {
        private static Main plugin;
        private CommandManager commandMgr;
        public void onEnable() {
            // Declaring variables
            plugin = this;
            // Configuration version handler
            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
            // Start reading events of the server
            // Start Cooldown mechanisms
            commandMgr = new CommandManager(this);
    And this is the CommandManager class

    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.

            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


    @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