Solved ScoreBoard(NameTag) Error on Player Join

Discussion in 'Spigot Plugin Development' started by _zMattyPower_, Aug 5, 2018.

  1. Hi, I Created a Scoreboard with Nametag that updates itself every second. The problem is that i get this error:

    Code (Text):
    [23:36:21 WARN]: [Hub] Task #1634 for Hub v0.3 generated an exception
    java.lang.NullPointerException
            at me.matty.Functions.ScoreBoard$1.run(ScoreBoard.java:97) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:709) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8.jar:git-Spigot-550ebace-7019900e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_162]

    This is the code:

    Code (Java):
        public static void runNameTagUpdater(Player p) {
            new BukkitRunnable() {
                public void run() {
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        Scoreboard b = player.getScoreboard();
                        Team Founder = b.getTeam("Founder");
                        Team Youtuber = b.getTeam("Youtuber");
                        Team User = b.getTeam("User");
                        for (Player player2 : Bukkit.getOnlinePlayers()) {
                            if (MySQL.getRank(p).equals("Founder")) {
                                if (!(Founder.hasPlayer(p)))
                                    Founder.addPlayer(p);
                                if (Youtuber.hasPlayer(p))
                                    Youtuber.removePlayer(p);
                                if (User.hasPlayer(p))
                                    User.removePlayer(p);
                            } else if (MySQL.getRank(p).equals("Youtuber")) {
                                if (!(Youtuber.hasPlayer(p)))
                                    Youtuber.addPlayer(p);
                                if (Founder.hasPlayer(p))
                                    Founder.removePlayer(p);
                                if (User.hasPlayer(p))
                                    User.removePlayer(p);
                            } else if (MySQL.getRank(p).equals("User")) {
                                if (!(User.hasPlayer(p)))
                                    User.addPlayer(p);
                                if (Youtuber.hasPlayer(p))
                                    Youtuber.removePlayer(p);
                                if (Founder.hasPlayer(p))
                                    Founder.removePlayer(p);
                            } else {
                                return;
                            }
                            Founder.setPrefix("§cFounder §7");
                            Youtuber.setPrefix("§fYou§cTuber §7");
                            User.setPrefix("§7");
                        }
                    }
                }
            }.runTaskTimer(Main.getInstance(), 0, 1 * 20);
        }
     
  2. Would you kindly note which line is line 97 in the ScoreBoard.java file?

    From glancing at the stack trace, NullPointerException means you're trying to access a null object. Something on line 97 simply hasn't been initialized. If there's several objects, then you can print out to console if each is null, and then trace back where that object should be initialized.
     
  3. Ah sorry, the line 97 is "if (!(Founder.hasPlayer(p)))", the problem is that the error will be triggered only sometimes.
     
  4. I would venture to guess that sometimes Founder is null. Possibly the 'p' variable being null would also throw it, but I'm not sure about that. I would print out a debug message and check which is null
    Code (Java):
    if (Founder == null) { Bukkit.getLogger().log(Level.WARNING, "Founder is null"); }
    if (p == null) { Bukkit.getLogger().log(Level.WARNING, "Player is null"); }
     
  5. Ok, only founder is null sometimes. I added this to my code
    Code (Java):
                                if (Founder == null) {
                                    Bukkit.getLogger().log(Level.WARNING, "Founder is null");
                                    return;
                                }

    Sometimes is null but with 'return', it doesn't throw anymore the error and it works.
     
  6. This is like:
    You don't know why it's happening and you don't care...