NullPointerException with scoreboards

Discussion in 'Plugin Development' started by Starfire1337, Aug 7, 2014.

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

    Starfire1337

    Hello everyone, this bug is probably really stupid, but I can't seem to figure it out :/
    I am trying to make a minigame, and I want to display the time remaining on a scoreboard.
    Method to start countdown:
    Show Spoiler
    Code:java
    1. public void countdownArena(final int arena) {
    2. new BukkitRunnable() {
    3. int secondsLeft = 60;
    4. @SuppressWarnings("deprecation")
    5. @Override
    6. public void run() {
    7. HashMap<String, Integer> players = ArenaManager.getPlayers();
    8. for (String key : players.keySet()) {
    9. if(players.get(key) == arena) {
    10. Player p = Bukkit.getPlayer(key);
    11. ScoreboardManager sbMan = new ScoreboardManager();
    12. sbMan.updateScoreboard(p, secondsLeft); //<======= Line 202
    13. }
    14. }
    15. if(secondsLeft == 0) {
    16. for (String key : players.keySet()) {
    17. if(players.get(key) == arena) {
    18. Player p = Bukkit.getPlayer(key);
    19. p.sendMessage(ChatColor.YELLOW + "It looks like there were no winners this time :(");
    20. }
    21. }
    22. this.cancel();
    23. }
    24. secondsLeft--;
    25. }
    26. }.runTaskTimer(this, 0L, 20L);
    27. }

    And my scoreboard methods:
    Show Spoiler
    Code:java
    1. public class ScoreboardManager {
    2. @SuppressWarnings("deprecation")
    3. public void createScoreboard(Player p, int timer) {
    4. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    5. Objective objective = board.registerNewObjective("PvPRun", "dummy");
    6. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    7. Score time = objective.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Time Remaining:"));
    8. time.setScore(timer);
    9. p.setScoreboard(board);
    10. }
    11. @SuppressWarnings("deprecation")
    12. public void updateScoreboard(Player p, int timer) {
    13. if (p.getScoreboard().getObjective("PvPRun") != null) {
    14. Scoreboard b = p.getScoreboard();
    15. Objective ob = b.getObjective("PvPRun");
    16. Score time = ob.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Time Remaining:"));
    17. time.setScore(timer);
    18. }
    19. else {
    20. createScoreboard(p, timer);
    21. }
    22. }
    23. }
    24.  

    and the stack:
    Show Spoiler
    Code:
    [12:46:15] [Server thread/WARN]: [PvPRun] Task #16 for PvPRun v1.3 generated an exception
    java.lang.NullPointerException
        at com.starfire1337.PvPRun.PvPRun$2.run(PvPRun.java:202) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]


    So far, I've checked the timer is working properly by removing the call to the scoreboard methods and adding p.sendMessage(p.getName() + " - " + secondsLeft + " - " + key); which returned "Starfire1337 - seconds - Starfire1337" in-game, where seconds is the amount of seconds remaining.
     
  2. Offline

    mine-care

    what is line 202 in PvPRun class? (i assume countdown class)
     
  3. Offline

    Starfire1337

    mine-care
    I have it marked as //Line 202 on the code :p, AKA line 12 from the snip-it
     
  4. Offline

    mine-care

    :eek: didnt notice sorry :3
    i supose eather player or count is null but i cant find out how tht posible Mabe a more expirienced eye than mine could seek it out :3
     
  5. Offline

    Starfire1337

    mine-care
    I've used p.sendMessage, and neither are null :/
     
  6. Offline

    kps1796

    Starfire1337 sbMan is null? Did you set it to a new instance?
    i.e.
    Code:java
    1. ScoreboardManager sbMan = new ScoreboardManager();

    EDIT: I see you did that. Maybe try moving that line out of the for loop? Like at the beginning of the method?
     
  7. Offline

    mine-care

    kps1796 is right, see! a more expirienced eye!
     
    kps1796 likes this.
  8. Offline

    Starfire1337

    mine-care
    kps1796
    After moving it out:
    Code:
    [14:21:44] [Server thread/WARN]: [PvPRun] Task #84 for PvPRun v1.3 generated an exception
    java.lang.NullPointerException
        at com.starfire1337.PvPRun.ScoreboardManager.updateScoreboard(ScoreboardManager.java:23) ~[?:?]
        at com.starfire1337.PvPRun.PvPRun$2.run(PvPRun.java:202) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    and

    Code:java
    1. HashMap<String, Integer> players = ArenaManager.getPlayers();
    2. ScoreboardManager sbMan = new ScoreboardManager();
    3. for (String key : players.keySet()) {
    4. if(players.get(key) == arena) {
    5. Player p = Bukkit.getPlayer(key);
    6. sbMan.updateScoreboard(p, secondsLeft); //Line 202
    7. }
    8. }


    Line 23 on ScoreboardManager is Line 13 on the OP for ScoreboardManager

    Edit: I didn't see you say put it at the beginning of the method, I did that and came up with the same error.
     
  9. Offline

    kps1796

    Starfire1337
    I thought the NPE would be in updatescoreboard. I'm between lines 12 and 13, try logging things to find out what's null. For example, to check if the player is null, log their name. If the player is null you will get the npe on the log line.
    EDIT: before checking if the objective is not null, make sure getscoreboard doesnt return null.

    So instead of
    Code:java
    1. if (p.getScoreboard().getObjective("PvPRun") != null)

    Do
    Code:java
    1. if (p.getScoreboard() != null && p.getScoreboard().getObjective("PvPRun") != null)


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    Starfire1337

    kps1796
    Still getting a NPE even if I add that code :/

    Edit: I found the problem, it looks like the player was null inside the updateScoreboard method, but it's not null right before I call the updateScoreboard() method :/

    Edit 2: it seems like Player p = Bukkit.getServer().getPlayer(pl); is causing the problem, I am now passing String pl (Player name) and converting that to a player inside the method. The player name is passing as "Starfire1337" just fine.

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void updateScoreboard(String pl, int timer) {
    3. Bukkit.getServer().getLogger().info(pl); //this returns Starfire1337, as it normally should.
    4. Player p = Bukkit.getServer().getPlayer(pl);
    5. if (p.getScoreboard() != null && p.getScoreboard().getObjective("PvPRun") != null) {
    6. Scoreboard b = p.getScoreboard();
    7. Objective ob = b.getObjective("PvPRun");
    8. Score time = ob.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Time Remaining:"));
    9. time.setScore(timer);
    10. }
    11. else {
    12. createScoreboard(pl, timer);
    13. }
    14. }


    Edit 3:
    I put the whole thing in a try/catch statement, and it returned a NPE, also, It may be handy to know I get kicked from the server with the following error:

    Internal Exception: io.netty.handler.codec.DecoderException: java.IOException: The recieved string length is longer than maximum allowed (18 > 16)
     
  11. Offline

    kps1796

    Well I've never dealt with scoreboards before. You should probably ask someone more experienced with them
     
  12. Offline

    Starfire1337

    kps1796
    The error is not with scoreboards, the error is with the String pl not being able to be converted to Player p
     
  13. Offline

    kps1796

    Starfire1337 Is updateScoreboard in the class that extends JavaPlugin?
     
  14. Offline

    Starfire1337

    kps1796
    PvPRun extends JavaPlugin
     
  15. Offline

    kps1796

    Starfire1337 can you pass the instance of PvPRun to the scoreboard manager and then run plugin.getServer instead of Bukkit.getServer?
     
  16. Offline

    Starfire1337

    kps1796
    using
    Code:
    public ScoreboardManager sbMan = new ScoreboardManager(this);
    and
    Code:
    static PvPRun plugin;
    public ScoreboardManager(PvPRun pvpRun) {
            plugin = pvpRun;
        }
    
    Code:
    Player p = plugin.getServer().getPlayer(pl);
    And that's returning the same error, not sure if I did that correctly though
     
  17. Offline

    kps1796

    Starfire1337 You did it right. Which error are you getting - the NPE or the string length one?
     
  18. Offline

    Starfire1337

    kps1796
    NPE comes through the console, while string comes through my client.
     
  19. Offline

    kps1796

    Starfire1337 I have no idea what the string thing means.
     
  20. Offline

    Starfire1337

    kps1796
    I'm assuming the player name (as a string) is 18 characters, while it's only supposed to be up to 16. So I am assuming somehow the player name got messed up, although if I print the string, it outputs as "Starfire1337"

    Okay - This was actually an error with the scoreboard system. I had put: Time Remaining: 60, which was 18 characters long, and due to Minecraft's limitations, this could not be done, instead, changing it to Time Left: works :)

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

Share This Page