0

I am currently trying to make an updating scoreboard that tracks a players HP and current kills but I am not having much luck. I have tried many different things and been stuck for well over 2 hours now. The problem lies when trying to get the players HP and kills. The error below is that of the kills issue, but another issue is the HP returning null hence the Unhandled Exception. Here is there error:

enter image description here

Here is the code: Line 144 - 162 of Main.java

@SuppressWarnings("deprecation")
public void run(String arena){
    for(String key : Handler.playerMap.keySet()){
        if (Handler.playerMap.get(key).contains(arena)){
            Player pt = Bukkit.getPlayer(key);
            String p = pt.getDisplayName();
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
            Objective objective = board.registerNewObjective("test", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.AQUA + "Wave: " );
            Score score = objective.getScore(ChatColor.GREEN + "" + pt.getHealth() + ChatColor.WHITE + p + ChatColor.AQUA);
            int kills = PlayerHandler.kills.get(p);
            score.setScore(kills);
            int pcheck = 0;

Can anybody tell me what the problem is? Thank you in advance for any comments.

Ben Green
  • 3,953
  • 3
  • 29
  • 49

3 Answers3

0

As you have pointed out, this is happening when the PlayerHandler.kills returns null, or when getting the player HP returns null. The reason this is occuring is because you are trying to perform a method on a null object. So in the above example, when you try to get(p) on a null object, you get this exception.

You can avoid NullPointerExceptions by adding nullity checks to make sure that the object isn't null before calling a method on it. For example, the below would check whether there are any kills, and if not set the score to zero:

int kills = 0;
if (PlayerHandler.kills != null) {
    kills = PlayerHandler.kills.get(p);
}
score.setScore(kills);

You will have to do something similar to avoid the situation where the hp is null.

Take a look at this article about null pointers and how to avoid them for more details.

Ben Green
  • 3,953
  • 3
  • 29
  • 49
0

Disclaimer: I have no experience in Minecraft plugin development whatsoever. But I do have some experience in Minecraft Mod development.

I think the problem is that no player has killed anyone. The exception stack trace says that the exception occurred on line 160:

int kills = PlayerHandler.kills.get(p);

A NullPointerException indicates that something is null. In the line, only PlayerHandler.kills and p can be null. However, p is actually the player's display name and I don't think players can have null as the display name. So the possibility that p == null is eliminated.

Now only one thing can be null - the kills. I believe that kills is of type HashMap<String, Integer>. According to my common sense, this is a map that contains all the player names as keys and the number of kills that the player has as values.

So why is the hash map null? Maybe no one got any kills yet. I used to mess around with scoreboards in Minecraft, and I know that other than a 0 score, you can also "not have a score", which I think is represented as null in the source.

How to solve this:

A lot of ways!

  • Kill someone
  • Kill yourself by an arrow or potion (if you are FOREVER ALONE)
  • add a check to see whether the map is null (recommended)

Here is the check:

int kills = 0;
if (PlayerHandler.kills != null) {
    kills = PlayerHandler.kills.get(p);
}
score.setScore(kills);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Assuming that all the static variables are initialized correctly the line int kills = PlayerHandler.kills.get(p); can still cause a NullPointerException

This is because attempting to assign null to a primative variable will throw an NullPointerException

Integer test = null;
int thisWillThrowNPE = test;
Kiskae
  • 24,655
  • 2
  • 77
  • 74