NullPointerException

Discussion in 'Plugin Development' started by CheesyFreezy, Mar 17, 2015.

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

    CheesyFreezy

    Hi hi developers.
    So i was making my custom survival games plugin and suddenly my console gave a NullPointerException i have no idea why. This is my code:
    Code:
    package me.CheesyFreezy.Scoreboard;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Set;
    
    import me.CheesyFreezy.Main.Core;
    import me.CheesyFreezy.Main.Plugin;
    import me.CheesyFreezy.Saves.Files;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    @SuppressWarnings("unused")
    public class Scoreboard {
        private ScoreboardManager sbManager;
        private org.bukkit.scoreboard.Scoreboard Lscoreboard = null;
        private org.bukkit.scoreboard.Scoreboard SGscoreboard = null;
     
        private Objective Lobj = null;
        private Objective SGobj = null;
     
        private Score Lempty1 = null;
        private Score Lempty2 = null;
        private Score Lempty3 = null;
     
        private Score SGempty1 = null;
        private Score SGempty2 = null;
        private Score SGempty3 = null;
     
        private Score Lrank = null;
        private Score Lcredits = null;
        private Score Lstaff = null;
        private Score Lwebsite = null;
     
        private Score SGplayers = null;
        private Score SGclass = null;
        private Score SGtime = null;
        private Score SGwebsite = null;
     
        public int totalStaff = 0;
        private int time = -1;
     
        private HashMap<String, Integer> SGtotalPlayers = new HashMap<String, Integer>();
     
        @SuppressWarnings("deprecation")
        public void SetScoreboard(final Player player, ScoreboardType type) {
            if(type == ScoreboardType.LOBBY) {
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Plugin.core, new Runnable() {
                    public void run() {
                        ScoreboardManager sbManager = Bukkit.getServer().getScoreboardManager();
                        Lscoreboard = sbManager.getNewScoreboard();
                     
                        if(Lobj == null) {
                            Lobj = Lscoreboard.registerNewObjective("sb", "dummy");
                            Lobj.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "ArcticMC");
                            Lobj.setDisplaySlot(DisplaySlot.SIDEBAR);
                        } else {
                            Lobj = Lscoreboard.getObjective("sb");
                        }
                     
                        Lscoreboard.resetScores(player);
                     
                        File file = new File(Plugin.core.getDataFolder(), "economy.yml");
                        YamlConfiguration fileConfig = null;
                        if(file.exists()) {
                            fileConfig = YamlConfiguration.loadConfiguration(file);
                        }
                     
                        if(fileConfig != null) {
                            if(!fileConfig.isSet("credits." + player.getName())) {
                                fileConfig.set("credits." + player.getName(), 0);
                                try {
                                    fileConfig.save(file);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                     
                        String rank = ChatColor.stripColor(player.getDisplayName().replace(player.getName(), ""));
                        if(rank.isEmpty()) {
                            rank = "Default";
                        }
                     
                        Lrank = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.WHITE + "Rank: " + ChatColor.GREEN + rank + "     "));
                     
                        if(file.exists()) {
                            if(fileConfig.isConfigurationSection("credits")) {
                                if(fileConfig.isSet("credits." + player.getName())) {
                                    Lcredits = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Credits: " + ChatColor.GREEN + fileConfig.getInt("credits." + player.getName()) + "     "));
                                } else {
                                    try {
                                        file.createNewFile();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                 
                                    fileConfig = YamlConfiguration.loadConfiguration(file);
                                    fileConfig.set("credits." + player.getName(), 0);
                                    try {
                                        fileConfig.save(file);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                 
                                    Lcredits = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Credits: " + ChatColor.GREEN + "0     "));
                                }
                            } else {
                                try {
                                    file.createNewFile();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                             
                                fileConfig = YamlConfiguration.loadConfiguration(file);
                                fileConfig.set("credits." + player.getName(), 0);
                                try {
                                    fileConfig.save(file);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                             
                                Lcredits = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Credits: " + ChatColor.GREEN + "0     "));
                            }
                        } else {
                            try {
                                file.createNewFile();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                         
                            fileConfig = YamlConfiguration.loadConfiguration(file);
                            fileConfig.set("credits." + player.getName(), 0);
                            try {
                                fileConfig.save(file);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                         
                            Lcredits = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Credits: " + ChatColor.GREEN + "0     "));
                        }
                     
                        for(Player players : Bukkit.getOnlinePlayers()) {
                            if(players.hasPermission("arcticmc.helper") || players.hasPermission("arcticmc.admin") || players.isOp()) {
                                totalStaff++;
                            }
                        }
                     
                        Lstaff = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Staff Online: " + ChatColor.GREEN + totalStaff + "     "));
                        Lwebsite = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.WHITE + "  www.ArcticMc.com"));
                     
                        Lempty1 = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(""));
                        Lempty2 = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(" "));
                        Lempty3 = Lobj.getScore(Bukkit.getServer().getOfflinePlayer("  "));
                     
                        Lrank.setScore(6);
                        Lempty1.setScore(5);
                        Lcredits.setScore(4);
                        Lempty2.setScore(3);
                        Lstaff.setScore(2);
                        Lempty3.setScore(1);
                        Lwebsite.setScore(0);
                     
                        player.setScoreboard(Lscoreboard);
                     
                        totalStaff = 0;
                    }
                }, 1);
            }
            if(type == ScoreboardType.SG) {
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Plugin.core, new Runnable() {
                    public void run() {
                        ScoreboardManager sbManager = Bukkit.getServer().getScoreboardManager();
                        SGscoreboard = sbManager.getNewScoreboard();
                     
                        if(SGobj == null) {
                            SGobj = SGscoreboard.registerNewObjective("sg", "dummy");
                            SGobj.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "SurvivalGames");
                            SGobj.setDisplaySlot(DisplaySlot.SIDEBAR);
                        } else {
                            SGobj = SGscoreboard.getObjective("sg");
                        }
                     
                        SGscoreboard.resetScores(player);
                     
                        Files files = new Files();
                        YamlConfiguration arenaFile = YamlConfiguration.loadConfiguration(files.SGArenas);
                     
                        Set<String> keys = arenaFile.getKeys(false);
                        keys.remove("null");
                     
                        for(String arenaList : keys) {
                            SGtotalPlayers.put(arenaList, arenaFile.getInt(arenaList + ".data.PLAYER_COUNT"));
                        }
                     
                        for(String arenaList : arenaFile.getKeys(false)) {
                            SGplayers = SGobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Players: " + ChatColor.GREEN + SGtotalPlayers.get(arenaList) + "     "));
                        }
                        SGclass = SGobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Class: " + ChatColor.GREEN + Core.sgManager.getClass(player) + "     "));
                     
                        time = 600;
                        SGtime = SGobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW + "Time: " + ChatColor.GREEN + time + "     "));
                     
                        SGwebsite = SGobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.WHITE + "  www.ArcticMc.com"));
                     
                        SGempty1 = SGobj.getScore(Bukkit.getServer().getOfflinePlayer(""));
                        SGempty2 = SGobj.getScore(Bukkit.getServer().getOfflinePlayer(" "));
                        SGempty3 = SGobj.getScore(Bukkit.getServer().getOfflinePlayer("  "));
                     
                        SGplayers.setScore(6);
                        SGempty1.setScore(5);
                        SGclass.setScore(4);
                        SGempty2.setScore(3);
                        SGtime.setScore(2);
                        SGempty3.setScore(1);
                        SGwebsite.setScore(0);
                     
                        player.setScoreboard(SGscoreboard);
                    }
                }, 1);
            }
        }
    }
    Please help me ;)

    Line 93 and 205 are giving the NullPointerException
     
  2. Line 93
    Line 205
    Edit: Dude, you're using getOfflinePlayer. Of course there will be NPEs
     
  3. Offline

    CheesyFreezy

    So what should i use?
     
  4. I remember running into this with another developer. Actually use the string itself.
    Otherwise, there's another method that I don't remember correctly
     
  5. Offline

    CheesyFreezy

    The error still appears
     
  6. Offline

    Zombie_Striker

    Are you sure there is a player that connected to your server called "Rank:"? By getting an offline player, you are getting their player name.
     
  7. The 'Rank' is a fake playername. To add that to a scoreboard (in vanilla), you have to add that fake player to the scoreboard and add points. However, there isn't exactly a player called 'Rank: ' in his server, which is why he is using getOfflinePlayer
     
  8. Offline

    Zombie_Striker

    @Neonix getOfflinePlayer returns the player that Has connected to the server. If the string is not of a player that was on the server, then it returns null!
     
  9. Offline

    teej107

  10. Offline

    CheesyFreezy

  11. Offline

    Zombie_Striker

    Could lobJ or Sgobj be null? Do you set it to anything?
     
  12. Offline

    teej107

  13. Offline

    CheesyFreezy

    @teej107 that didn't help. I send a message to console and player with all the variables but none of them are returning null. It's really annoying coding like this, because whenever another error(1) appears, my console is being spammed with this error(2), and i can't find a solution for the error(1).

    EDIT: Now the error is not saying 'NullPointerException' but 'java.lang.IllegalArgumentException: OfflinePlayer cannot be null' Maybe this can help?
     
  14. you're doing something which makes it null....
    have you added a score to the players and rank?
    Actually,
    Lrank = Lobj.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.WHITE + "Rank: " + ChatColor.GREEN + rank + " "));
    You're getting the rank: and the rank itself? Shouldn't you only get rank
    Lrank = Lobj.getScore(Bukkit.getServer().getOfflinePlayer("rank"));
     
  15. Offline

    CheesyFreezy

    @Neonix, go to line 90. rank is just a string variable. I debugged it and it's not returning null
     
  16. Offline

    nverdier

  17. Offline

    CheesyFreezy

    @nverdier no variables are null. That's why i don't get why it's a NullPointerException
     
  18. Offline

    teej107

    Your stacktrace says otherwise. Now who are you going to believe? The program or yourself?
    It would be a lot easier to read your code if you followed the Java naming conventions. Put in debug code and print out which variables are null or not.
    Code:
    System.out.println("thisField: " + (thisField != null));
     
  19. Offline

    CheesyFreezy

    All are not returning null. There are returning like they have to
     
  20. Offline

    nverdier

    @CheesyFreezy Well you obviously missed one. Please post the code you used to check it.
     
Thread Status:
Not open for further replies.

Share This Page