Java OnEnable()中的NullPointerException(Bukkit插件)

Java OnEnable()中的NullPointerException(Bukkit插件),java,plugins,minecraft,bukkit,Java,Plugins,Minecraft,Bukkit,我的错误日志显示在第13行(在我的MainClass中)有一个NPE 我的主要班级: 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 Fi

我的错误日志显示在第13行(在我的MainClass中)有一个NPE

我的主要班级:

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() {

    }

}
还有我的另一门课:

getCommand如果命令不存在,则返回null。确保创建了“a”命令。

getCommand如果该命令不存在,则返回null。确保创建了“a”命令。

正如其他人所说的,您得到了一个NPE,因为命令“a”不存在。如果您还没有这样做,请将其添加到.yml文件中

commands:
   a:
     description: does something
     usage: /a
编辑:显然你也没有把它添加到你的onCommand中。使用以下命令检查命令

if(cmd.getName().equalsIgnoreCase("a")) {
    //do stuff when /a is executed
}
可以在那里检查
args
。例如:

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
    }
}

正如其他人已经说过的,您得到一个NPE是因为命令“a”不存在。如果您还没有这样做,请将其添加到.yml文件中

commands:
   a:
     description: does something
     usage: /a
编辑:显然你也没有把它添加到你的onCommand中。使用以下命令检查命令

if(cmd.getName().equalsIgnoreCase("a")) {
    //do stuff when /a is executed
}
可以在那里检查
args
。例如:

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
    }
}


可能getCommand返回null:为什么人们仍然问NPE问题?测试有那么难吗?是的。。回顾这一点,我不知道我在做什么。可能getCommand返回null:为什么人们仍然问NPE问题?测试有那么难吗?是的。。回想起来,我不知道自己在做什么。我忘记了如何将命令设为“a”。我该怎么把它放到我的另一门课上??(我已经有一段时间没做过bukkit了!)我忘记了如何将命令设置为“a”。我该怎么把它放到我的另一门课上??(我有一段时间没有做过bukkit!)我所要做的就是在plugin.yml中注册我的命令,因为我使用了CommandExecutor,所以我不必做的就是(label.equalsIgnoreCase(“a”)哦,我明白了。为我的编辑感到抱歉,我刚刚意识到我写的是胡说八道!别担心,我也是胡说八道,因为我应该先检查我的命令是否已注册!!编辑:好的,到底发生了什么,我会发布错误日志:OThat是我的错,应该保留编辑。你仍然需要检查onCom中的实际命令mand方法,正如我在回答中所写的那样。这不会有什么帮助,现在你只是在胡说八道:我必须做的是在plugin.yml中注册我的命令,因为我使用了CommandExecutor,所以我不必做is(label.equalsIgnoreCase(“a”)哦,我明白了。为我的编辑感到抱歉,我刚刚意识到我写的是胡说八道!别担心,我也是胡说八道,因为我应该先检查我的命令是否已注册!!编辑:好的,到底发生了什么,我会发布错误日志:OThat是我的错,应该保留编辑。你仍然需要检查onCom中的实际命令曼德方法,正如我在回答中所写的。那是没有用的,现在你只是在说废话:P