即使在bukkit / spigiot插件中进行空检查,NPE也会被抛出

时间:2016-01-16 17:00:06

标签: java nullpointerexception bukkit

我正在开发的spigiot(基于bukkit)插件出错。我关注了一个关于迷你游戏插件的代码教程并下载了源代码(),该代码工作得很好。之后我编辑了代码并添加了自己的武器并开始开发一个多玩家生成系统。现在,当我离开游戏时,我得到一个空指针异常,即使我在第一次获得这个之后进行了检查。

[11:36:49] [Server thread/INFO]: Zippynik issued server command: /mb l 1[11:36:49] [Server thread/ERROR]: nullorg.bukkit.command.CommandException: Unhandled exception executing command 'mb' in plugin MagicBattle v1.2.0at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_80]at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_80]at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]at java.lang.Thread.run(Unknown Source) [?:1.7.0_80]Caused by: java.lang.NullPointerExceptionat me.pogostick29dev.magicbattle.PlayerData.restorePlayer(Arena.java:212) ~[?:?]at me.pogostick29dev.magicbattle.Arena.removePlayer(Arena.java:86) ~[?:?]at me.pogostick29dev.magicbattle.commands.Leave.onCommand(Leave.java:17) ~[?:?]at me.pogostick29dev.magicbattle.CommandManager.onCommand(CommandManager.java:65) ~[?:?]at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8-R0.1.jar:git-Bukkit-d3e0b6f]... 15 more

继承PlayerData类的代码(Arena.java的一部分)

    public int playerNumber;
    private Player puu;
    private String playerName;
    private ItemStack[] contents, armorContents;
    private Location location;

    protected PlayerData(Player p,int pnum) {
        this.playerName = p.getName();
        this.contents = p.getInventory().getContents();
        this.armorContents = p.getInventory().getArmorContents();
        this.location = p.getLocation();
        this.playerNumber = pnum;
        this.puu = p;
    }

    protected Player getPlayer() {
        return puu;
    }

    protected void restorePlayer() {
        Player p;
        if (puu != null){
         p = puu;}
        else{

            Bukkit.broadcastMessage("puu is invalid");
            return;
        }
        if (contents !=null){
        p.getInventory().setContents(contents);}
        if (armorContents !=null){
        p.getInventory().setArmorContents(armorContents);}
        if (location !=null){
        p.teleport(location);}
        return;
        }


    protected boolean isForPlayer(Player p) {
        return playerName.equalsIgnoreCase(p.getName());
    }


}

,关于离开竞技场的竞技场类代码。

public void removePlayer(Player p) {
        if(getPlayerData(p) !=null){
        PlayerData d = getPlayerData(p);
        d.restorePlayer();
        data.remove(d);
        }else{
            Bukkit.broadcastMessage("PlayerData is null");
        }
        currentPlayers--;

        for (Sign s : signs) {
            s.setLine(2, currentPlayers + "");
            s.update();
        }

        if (currentPlayers == 1) {
            stop(data.get(0).getPlayer());
        } else if (currentPlayers == 0) {
            stop();
        }
    }

and finally for Leave.java

        public class Leave extends MagicCommand {

        public void onCommand(Player p, String[] args) {
            if (ArenaManager.getInstance().getArena(p) == null) {
                MessageManager.getInstance().msg(p, MessageType.BAD, "You are not already in an arena!");
                return;
            }

            ArenaManager.getInstance().getArena(p).removePlayer(p);
        }
        public Leave() {
            super("Leave an arena.", "", "l");
        }
    }

如果你还需要其他东西,请点击github https://github.com/Zippynik/MagicBattle

编辑:212 restorePlayer的开始 86 RemovePlayer的开始 17:ArenaManager.getInstance()的开始.getArena(p).removePlayer(p);

编辑2:为restorePlayer和removePlayer中使用的所有变量添加了空检查...仍然返回null。问题可能是ArenaManager.getInstance()或getArena()

0 个答案:

没有答案