HashMap ValueComparator kills

Discussion in 'Spigot Plugin Development' started by ElbestHD, Mar 29, 2019.

  1. Hello people from spigot, I have a problem with this code to see if you can help me, excuse my English
    code:
    Code (Text):
    public static HashMap<UUID, Integer> kills;

    public void getTopKills(Player p) {
            TreeMap<UUID, Object> treeMap = new TreeMap<UUID, Object>(new ValueComparator(this.getKills()));
            treeMap.clear();
            treeMap.putAll(getKills());
            int n = 0;
            p.sendMessage("§7&m---------------------------");
            p.sendMessage("§6&lTOP 5 KILLS");
            p.sendMessage("§7");
            for (final UUID uuid : treeMap.keySet()) {
                if (n > 9) {
                    break;
                }
                if (uuid == null) {
                    continue;
                }
                p.sendMessage(String.valueOf(String.valueOf("§b")) + (n + 1) + ") " + "§6" + Bukkit.getOfflinePlayer(uuid).getName() + " - " + "§a" + treeMap.get(uuid));
                ++n;
            }
            p.sendMessage("§8");
            p.sendMessage("§7&m---------------------------");
        }

    public void addKillPlayer(Player player) {
            int intValue = 0;
            if (Tool.kills.containsKey(player.getUniqueId())) {
                intValue = Tool.kills.get(player.getUniqueId());
            }
            Tool.kills.put(player.getUniqueId(), intValue + 1);
        }
       
        public HashMap<UUID, Integer> getKills() {
            return Tool.kills;
        }

    command:
    Code (Text):
    public class LeaderBoardCommand implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(BuildFFA.get().getConfig().getString("MESSAGES.COMMAND-ERROR").replace("&", "§"));
                return true;
            }
            if (command.getName().equalsIgnoreCase("leaderboard")) {
                Player p = (Player)sender;
                Tool.get().getTopKills(p);
                return true;
            }
            return false;
        }

    }
     

    EventHandler:
    Code (Text):
    @EventHandler
        public void onPlayerDeathEvent(PlayerDeathEvent e) {
            e.setDroppedExp(0);
            Player p = e.getEntity().getPlayer();
            Player k = e.getEntity().getKiller();
                   Tool.get().addKillPlayer(p);
    }

    Error:
    Code (Text):
    [22:17:34] [Server thread/INFO]: xsh4dex issued server command: /leaderboard
    [22:17:34] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'leaderboard' in plugin BuildFFA v3.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    Caused by: java.lang.NullPointerException
        at me.theedwardvz.buildffa.utils.Tool.getTopKills(Tool.java:91) ~[?:?]
        at me.theedwardvz.buildffa.commands.LeaderBoardCommand.onCommand(LeaderBoardCommand.java:21) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        ... 13 more
     
  2. Code (YAML):
      at me.theedwardvz.buildffa.utils.Tool.getTopKills(Tool.java:91) ~[?:?]
    As you can see, the problem become to Tool. Java if im right.

    This line: Tool.get().getTopKills(p);
    In your command line 21, id the probleme she return null.
     
  3. upload_2019-3-28_23-42-50.png
    class of the error
     

    Attached Files:

  4. drives_a_ford

    Moderator

    I seriously doubt it's that line you get your NPE from. You've already used a method of treeMap successfully and if getKills returned null, you'd get your NPE in TreeMap#putAll. You've most likely changed your code after compiling and getting the stacktrace.

    With that said, it's easy to add debug messages within the getTopKills method to see where you get in that method and also output variables to see which one is null.

    When looking at your getKills method, I can see you're abusing static in your Tool class as well. Please take a look at section 1 of Beginner Programming Mistakes and Why You're Making Them. Once you've got that done, take a look at section 3.2.
    If you want to use the singleton pattern (which you seem to be trying at points), do that. But don't mix in other static methods.
     
    • Agree Agree x 1