Null Error help

Discussion in 'Plugin Development' started by Duuckky, May 18, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Duuckky

    Having an NullPointer exception here cannot figure out why

    Code:java
    1. Player p = (Player) sender;
    2. API.setStats(p);
    3.  
    4. try {
    5. StatTrack.kills.put(p.getUniqueId(), plugin.getConfig().getInt("kills." + p.getUniqueId()));
    6. StatTrack.deaths.put(p.getUniqueId(), plugin.getConfig().getInt("deaths." + p.getUniqueId()));
    7. StatTrack.points.put(p.getUniqueId(), plugin.getConfig().getInt("points." + p.getUniqueId()));
    8. } catch (NullPointerException ne) {
    9. System.out.println("Null Pointer Exception");
    10. }
     
  2. Offline

    Nghtmr9999

    StatTrack could be null, but without the stacktrace it's hard to tell. Could you post the stacktrace?
     
  3. Offline

    Duuckky

    Code:
    [18:14:15 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stat
    s' in plugin StatTrack vv1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:703) ~[craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
     
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:955) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078
    jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.NullPointerException
            at me.duuckky.commands.ShowStats.onCommand(ShowStats.java:22) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            ... 13 more
    Code:java
    1. package me.duuckky.commands;
    2.  
    3. import me.duuckky.API;
    4. import me.duuckky.StatTrack;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class ShowStats implements CommandExecutor {
    12.  
    13. public StatTrack plugin;
    14.  
    15. @Override
    16. public boolean onCommand(CommandSender sender, Command cmd, String label,
    17. String[] args) {
    18.  
    19. Player p = (Player) sender;
    20. API.setStats(p);
    21.  
    22. StatTrack.kills.put(p.getUniqueId(), plugin.getConfig().getInt("kills." + p.getUniqueId()));
    23. StatTrack.deaths.put(p.getUniqueId(), plugin.getConfig().getInt("deaths." + p.getUniqueId()));
    24. StatTrack.points.put(p.getUniqueId(), plugin.getConfig().getInt("points." + p.getUniqueId()));
    25.  
    26. if(sender instanceof Player) {
    27.  
    28. if(args.length == 1) {
    29. API.showStats(p, args[0]);
    30. return true;
    31. } else if(args.length == 0) {
    32. API.showStats(p, p.getName());
    33. return true;
    34. }
    35. }
    36. return false;
    37. }
    38. }
     
  4. Offline

    nlthijs48

    The error is at line 22, so probably StatTrack is null or the kills variable in StatTrack is null. Check it at line 21 and print out a message if it is null.

    Edit: pretty sure StatTrack is null, you never assign a value to it, you will need a constructor to do that.
     
  5. Offline

    Duuckky

    I added a check

    Code:java
    1. package me.duuckky.commands;
    2.  
    3. import me.duuckky.API;
    4. import me.duuckky.StatTrack;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class ShowStats implements CommandExecutor {
    12.  
    13. public StatTrack plugin;
    14.  
    15. @Override
    16. public boolean onCommand(CommandSender sender, Command cmd, String label,
    17. String[] args) {
    18.  
    19. Player p = (Player) sender;
    20.  
    21. API.setStats(p);
    22.  
    23. if(StatTrack.kills == null ) {
    24. System.out.println("null");
    25. }
    26.  
    27. StatTrack.kills.put(p.getUniqueId(), plugin.getConfig().getInt("kills." + p.getUniqueId()));
    28. StatTrack.deaths.put(p.getUniqueId(), plugin.getConfig().getInt("deaths." + p.getUniqueId()));
    29. StatTrack.points.put(p.getUniqueId(), plugin.getConfig().getInt("points." + p.getUniqueId()));
    30.  
    31. if(sender instanceof Player) {
    32.  
    33. if(args.length == 1) {
    34. API.showStats(p, args[0]);
    35. return true;
    36. } else if(args.length == 0) {
    37. API.showStats(p, p.getName());
    38. return true;
    39. }
    40. }
    41. return false;
    42. }
    43. }


    now i get the error

    Code:
    [18:37:42 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stat
    s' in plugin StatTrack vv1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:703) ~[craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
     
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:955) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078
    jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.NullPointerException
            at me.duuckky.commands.ShowStats.onCommand(ShowStats.java:21) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit-1.7.9.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
            ... 13 more
    Thanks! Got it c:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page