如何修复Bukkit插件中的java.lang.NullPointerException:null?

时间:2019-01-06 17:07:43

标签: java minecraft bukkit

无法理解如何解决该错误,这对我来说是不真实的(找不到我的代码中的空白):

java.lang.NullPointerException: null
    at survival.MainClass.onEnable(MainClass.java:16) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot.jar:git-Spigot-dcd1643-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]

16行是:

getCommand(commands.cmd3).setExecutor(commands);

命令类:

public class Commands extends CommandExecute implements CommandExecutor, Listener {

public String cmd1 = "fly";
public String cmd2 = "feed";
public String cmd3 = "heal";

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
        if (cmd.getName().equalsIgnoreCase(cmd3)) {
            Player p = (Player) sender;
            if (p.getHealth() < 20) {
                p.setHealth(20);
                p.sendMessage(ChatColor.GOLD + "Healed succesfuly!");
                return true;
            } else {
                p.sendMessage(ChatColor.RED + "You are already healed!");
                return true;
            }

        }
        return false;
    } else {
        sender.sendMessage("Only for Players!");
        return true;
    }

  }

}

MainClass:

public class MainClass extends JavaPlugin implements Listener{
private Commands commands = new Commands();

public void onEnable() {
    getServer().getPluginManager().registerEvents(new MainHandler(), this);
    getCommand(commands.cmd1).setExecutor(commands);
    getCommand(commands.cmd2).setExecutor(commands);
    getCommand(commands.cmd3).setExecutor(commands);
  }
}

在此之前具有cmd1和cmd2,它们工作正常,只有cmd3命令有问题。

在plugin.yml中:

name: Survival
main: survival.MainClass
version: 1.1
commands:
  fly:
    description: Toggle flight mode
    usage: /fly
  feed:
    description: Feeds player
    usage: /feed
  heal:
    description: Heals player
    usage: /heal

尝试了关于stackoverflow的所有答案,但没有任何帮助

1 个答案:

答案 0 :(得分:0)

我终于做到了!

我改变了

private Commands commands = new Commands();

private Commands commands;

并在 onEnable()之后设置命令变量的值,现在在查找:

public void onEnable() {
    commands = new Commands();
    getServer().getPluginManager().registerEvents(new MainHandler(), this);
    getCommand(commands.cmd1).setExecutor(commands);
    getCommand(commands.cmd2).setExecutor(commands);
    getCommand(commands.cmd3).setExecutor(commands);