无论如何都保持在同一行的 NullPointerException

NullPointerException that stays at the same line no matter what

我正在尝试编写这个 Minecraft 插件的代码,但我一直在 CompMeme class(第 16 行)的 onEnable() 处收到 NullPointerException,奇怪的是即使我在同一行放置 System.out.print() 中。如果 NullPointerException 不存在,则意味着插件将简单地加载而没有任何更多消息。如何修复此 NullPointerException?

[21:51:15] [Server thread/ERROR]: Error occurred while enabling CompMeme v0.1.5-SNAPSHOT (Is it up to date?)
java.lang.NullPointerException
    at escaperestart.sircomputer.compmeme.CompMeme.onEnable(CompMeme.java:16) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.java:356) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.java:316) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.q(MinecraftServer.java:402) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.k(MinecraftServer.java:370) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.a(MinecraftServer.java:325) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.java:211) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:505) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Thread.java:744) [?:1.7.0_51

我不确定我做错了什么,这是我的 CompMeme class:

public final class CompMeme extends JavaPlugin {

    public static CompMeme plugin;
    public String version = "0.1.6-SNAPSHOT";

    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();

        getCommand("rofl").setExecutor(Executor.rofl);
        getCommand("facepalm").setExecutor(Executor.facepalm);
        getCommand("lol").setExecutor(Executor.lol);
        getCommand("iliketrains").setExecutor(Executor.iliketrains);
        getCommand("blamenill").setExecutor(Executor.blamenill);
        getCommand("odns").setExecutor(Executor.odns);
        getCommand("ragequit").setExecutor(Executor.ragequit);
        getCommand("rage").setExecutor(Executor.rage);
        getCommand("compmeme").setExecutor(Executor.compmemecmd);
    }

    public void onDisable() {
        saveConfig();
    }
}

这是我的 Executor class:

public class Executor {
    public static CompMeme compmeme = CompMeme.plugin;

    public static BasicCommand rofl = new BasicCommand(compmeme, "rofl", ChatColor.DARK_GREEN, "[Rofl]");
    public static BasicCommand facepalm = new BasicCommand(compmeme, "facepalm", ChatColor.AQUA, "[Facepalm]");
    public static BasicCommand lol = new BasicCommand(compmeme, "lol", ChatColor.DARK_GREEN, "[Lol]");
    public static BasicCommand iliketrains = new BasicCommand(compmeme, "iliketrains", ChatColor.DARK_PURPLE, "[Trains!]");
    public static BasicCommand blamenill = new BasicCommand(compmeme, "blamenill", ChatColor.DARK_RED, "[Blame Nill]");
    public static InputCommand odns = new InputCommand(compmeme, "odns", ChatColor.GOLD, "[ODNS]");
    public static Rage rage = new Rage(compmeme);
    public static Ragequit ragequit = new Ragequit(compmeme);
    public static CompMemeCmd compmemecmd = new CompMemeCmd(compmeme);
}

这是我的 plugin.yml:

name: CompMeme
main: escaperestart.sircomputer.compmeme.CompMeme
version: 0.1.5-SNAPSHOT
description: Meme commands for EscapeRestart
author: SirComputer

commands:
  rofl:
   description: Makes you roll on the floor laughing.
   usage: /rofl
   permission: compmeme.rofl

  facepalm:
   description: Makes you facepalm.
   usage: /facepalm
   permission: compmeme.facepalm

   lol:
    description: Makes you laugh out loud.
    usage: /lol
    permission: compmeme.lol

   iliketrains:
    description: Show your support for trains.
    usage: /iliketrains
    permission: compmeme.lol
    aliases: trains

   blamenill:
    description: Makes you blame Nill.
    usage: /blamenill
    permission: compmeme.blamenill

   odns:
    description: One does not simply...
    usage: /odns
    permission: compmeme.odns

   ragequit:
    description: Makes you ragequit.
    usage: /ragequit
    permission: compmeme.ragequit

   rage:
    description: Activate or deactivate rage mode.
    usage: /rage [activate/deactivate]
    permission: compmeme.rage
    aliases: ragemode

   compmeme:
    description: CompMeme information command.
    usage: /compmeme <info/enable/disable>
    permission: compmeme.compmeme
    aliases: ragemode

permissions:
  compmeme.*:
    default: op
    description: Ops get ALL the commands!
    children:
     compmeme.rofl: true
     compmeme.facepalm: true
     compmeme.lol: true
     compmeme.iliketrains: true
     compmeme.blamenill: true
     compmeme.odns: true
     compmeme.ragequit: true
     compmeme.rage: true
     compmeme.compmeme: true
     compmeme.enable: true
     compmeme.disable: true

我不确定是否有其他人在 Minecraft 插件中发生过这种情况 - 看起来它在其他事情中发生过一次或两次。

这一行

at escaperestart.sircomputer.compmeme.CompMeme.onEnable(CompMeme.java:16) ~[?:?]

表示。

1.) Class escaperestart.sircomputer.compmeme.CompMeme,

Method `onEnable()`,

Line 16

2.) 来到 java 文件的第 16 行,

getCommand("lol").setExecutor(Executor.lol);

可能是 - getCommand("lol") 返回 null。

请调试这里,不知何故问题只发生在这里。

当您尝试 运行 对象上的方法 null 时抛出 NullPointerException

您的例外说明:

at escaperestart.sircomputer.compmeme.CompMeme.onEnable(CompMeme.java:16) ~[?:?]

意思是CompMeme.java的第16行有一个NullPointerException,就是这一行:

getCommand("rofl").setExecutor(Executor.rofl);

这似乎并不是 getCommand("rofl") 的问题,而是您的 plugin.yml 没有正确缩进。这是我将其粘贴到 YAML to JSON parser:

时得到的结果
{
  "commands": {
    "facepalm": {
      "blamenill": {
        "usage": "/blamenill", 
        "description": "Makes you blame Nill.", 
        "permission": "compmeme.blamenill"
      }, 
      "description": "Makes you facepalm.", 
      "ragequit": {
        "usage": "/ragequit", 
        "description": "Makes you ragequit.", 
        "permission": "compmeme.ragequit"
      }, 
      "permission": "compmeme.facepalm", 
      "odns": {
        "usage": "/odns", 
        "description": "One does not simply...", 
        "permission": "compmeme.odns"
      }, 
      "rage": {
        "usage": "/rage [activate/deactivate]", 
        "permission": "compmeme.rage", 
        "description": "Activate or deactivate rage mode.", 
        "aliases": "ragemode"
      }, 
      "lol": {
        "usage": "/lol", 
        "description": "Makes you laugh out loud.", 
        "permission": "compmeme.lol"
      }, 
      "iliketrains": {
        "usage": "/iliketrains", 
        "permission": "compmeme.lol", 
        "description": "Show your support for trains.", 
        "aliases": "trains"
      }, 
      "usage": "/facepalm", 
      "compmeme": {
        "usage": "/compmeme <info/enable/disable>", 
        "permission": "compmeme.compmeme", 
        "description": "CompMeme information command.", 
        "aliases": "ragemode"
      }
    }, 
    "rofl": {
      "usage": "/rofl", 
      "description": "Makes you roll on the floor laughing.", 
      "permission": "compmeme.rofl"
    }
  }, 
  "permissions": {
    "compmeme.*": {
      "default": "op", 
      "description": "Ops get ALL the commands!", 
      "children": {
        "compmeme.odns": true, 
        "compmeme.facepalm": true, 
        "compmeme.iliketrains": true, 
        "compmeme.blamenill": true, 
        "compmeme.rofl": true, 
        "compmeme.ragequit": true, 
        "compmeme.rage": true, 
        "compmeme.compmeme": true, 
        "compmeme.disable": true, 
        "compmeme.lol": true, 
        "compmeme.enable": true
      }
    }
  }
}

如您所见,plugin.yml中lolcompmeme的命令缩进了1space太多了。要解决此问题,您应该将 plugin.yml 更改为:

commands:
  rofl:
   description: Makes you roll on the floor laughing.
   usage: /rofl
   permission: compmeme.rofl

  facepalm:
   description: Makes you facepalm.
   usage: /facepalm
   permission: compmeme.facepalm

  lol:
   description: Makes you laugh out loud.
   usage: /lol
   permission: compmeme.lol

  iliketrains:
   description: Show your support for trains.
   usage: /iliketrains
   permission: compmeme.lol
   aliases: trains

  blamenill:
   description: Makes you blame Nill.
   usage: /blamenill
   permission: compmeme.blamenill

  odns:
   description: One does not simply...
   usage: /odns
   permission: compmeme.odns

  ragequit:
   description: Makes you ragequit.
   usage: /ragequit
   permission: compmeme.ragequit

  rage:
   description: Activate or deactivate rage mode.
   usage: /rage [activate/deactivate]
   permission: compmeme.rage
   aliases: ragemode

  compmeme:
   description: CompMeme information command.
   usage: /compmeme <info/enable/disable>
   permission: compmeme.compmeme
   aliases: ragemode

permissions:
  compmeme.*:
    default: op
    description: Ops get ALL the commands!
    children:
     compmeme.rofl: true
     compmeme.facepalm: true
     compmeme.lol: true
     compmeme.iliketrains: true
     compmeme.blamenill: true
     compmeme.odns: true
     compmeme.ragequit: true
     compmeme.rage: true
     compmeme.compmeme: true
     compmeme.enable: true

lolcompmeme 的命令开头删除了一个 space,并且还为 usage 删除了一个 space,permissiondescriptionaliases 部分命令在 plugin.yml.

您应该始终将 plugin.yml 粘贴到 YAML 解析器中,例如 http://yaml-online-parser.appspot.com/ 以确保没有错误。