Search code examples
javapluginsminecraftbukkit

NullPointerException in OnEnable() (Bukkit plugin)


My error log is saying on line 13 (In my MainClass) there is an NPE.

My MainClass:

package me.p250;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import me.p250.command.BuyCommand;

public class MainClass extends JavaPlugin {

public FileConfiguration config;

public void onEnable() {
    getCommand("a").setExecutor(new BuyCommand(this));
}

public void onDisable() {

    }

}

And my other other class: http://pastebin.com/bYpCnPN2


Solution

  • As others already said, you get a NPE because the command "a" doesn't exist. If you haven't done that already, add it to your .yml file also.

    commands:
       a:
         description: does something
         usage: /a
    

    Edit: Apparently you haven't added it to your onCommand either. Check for the command using

    if(cmd.getName().equalsIgnoreCase("a")) {
        //do stuff when /a is executed
    }
    

    The check for args can be done there. Example:

    if(cmd.getName().equalsIgnoreCase("a")) {
        if(args[0].equalsIgnoreCase("test1")){
            //execute code for /a test1
        } else if(args[0].equalsIgnoreCase("test2")){
            //execute code for /a test2
        }
    }