0

I am getting an error on my spigiot(based on bukkit) plugin that I was developing. I followed a code tutorial on a minigame plugin and downloaded the source code()which worked fine. I edited the code after and added my own weapon and started developing a multi-player spawn system. Now, when I left the game, I get a null pointer exception, even with the checks I put in place after getting this the first time.

[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

Heres the code for the PlayerData class(part of 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());
    }


}

, for the Arena class code concerning leaving the arena.

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");
        }
    }

if you need anything else, its on github https://github.com/Zippynik/MagicBattle

Edit: 212 the beggining of restorePlayer 86 the beggining of RemovePlayer 17: Beginning of ArenaManager.getInstance().getArena(p).removePlayer(p);

Edit 2: Added null checks for all variables used in restorePlayer and removePlayer... Still returning null. Could the problem be ArenaManager.getInstance() or getArena()

  • 1
    Always, always, always say which line in the code you'r showing is the line the exception is complaining about. Which line in `restorePlayer is line 212? And line 86 of `removePlayer`? (Remember questions on SO must be **self-contained**, so "You can see that on github" is not the answer.) – T.J. Crowder Jan 16 '16 at 17:06
  • At a guess, I'd say `getPlayerData` is returning `null`, and so `d.restorePlayer();` throws an NPE. But since you haven't included `getPlayerData`... – T.J. Crowder Jan 16 '16 at 17:08
  • Nearly all "why am I getting an NPE?" questions are answered by [*What is a Null Pointer Exception, and how do I fix it?*](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). If after reading and trying the things there you don't think your answer is there, post a new question linking to that and saying very, very specifically *why* it didn't help. – T.J. Crowder Jan 16 '16 at 17:10
  • @T.J.Crowder to be fair, sometimes it is not as obvious, for example, when a library throws an NPE when a null parameter is passed, and not been documented. Not in this case, but some aren't so "1, 2, 3" – insou Jan 18 '16 at 11:18
  • 1
    @insou: Indeed, which is why I said to repost if having tried the information there, they still didn't track it down. Re the library method, that's where the link from the top answer there (finding the exact location in a stack trace) comes in. :-) – T.J. Crowder Jan 18 '16 at 11:29
  • http://stackoverflow.com/questions/34860921/unknown-npe-in-bukkit-plugin –  Jan 18 '16 at 18:21
  • And it is pretty confusing that in 2 instances the stack trace leads to the FIRST LINE OF A METHOD (the public void foo(int bar) etc.) –  Jan 18 '16 at 18:22

0 Answers0