Solved Why hashmap return null?

Discussion in 'Plugin Development' started by mAndAle, Jul 1, 2020.

  1. Offline

    mAndAle

    good evening, I would like to know the reason for the return null (I state that the hashmap when I go out is not empty), I attach the two codes

    Code:
    private HashMap<UUID, Integer> codeMap = new HashMap<UUID, Integer>();
        private HashMap<Integer, UUID> uuidMap = new HashMap<Integer, UUID>();
    
    @Override
            public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
            
                if (cs instanceof Player) {
                    Player p = (Player) cs;
                     if (args.length == 0) {
                     
                         if (this.codeMap.containsKey(p.getUniqueId())) {
                             p.sendMessage("TODO");
                         } else {
                             int randomcode = new Random().nextInt(800000)+200000;
                             p.sendMessage("codice:" + randomcode);
                             codeMap.put(p.getUniqueId(), randomcode);
                             uuidMap.put(randomcode, p.getUniqueId());
                             return true;
                         }
                     
                     } else {
                         p.sendMessage("TODO");
                         return true;
                     }
                } else {
                    cs.sendMessage("TODO");
                    return true;
                }
                return false;
            }
    event code:

    Code:
            @EventHandler
            public void onQuit(PlayerQuitEvent e) {
                this.codeMap.remove(e.getPlayer().getUniqueId());
                int code = this.codeMap.get(e.getPlayer().getUniqueId());
                this.uuidMap.remove(code);
            
            }
            

    error:

    Code:
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerList.disconnect(PlayerList.java:405) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1104) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.NetworkManager.handleDisconnection(NetworkManager.java:321) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.ServerConnection.c(ServerConnection.java:174) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:845) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at org.mAndAle.Tokens.discord.BotCreator.onQuit(BotCreator.java:59) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        ... 12 more

    it seems as if the hashmap were empty (even if with the command I put inside the player uuid)
     
    Last edited: Jul 1, 2020
  2. Offline

    timtower Moderator Moderator

  3. Offline

    mAndAle

    sorry i'll post the error
     
  4. Offline

    KarimAKL

    You didn't give us the line numbers so, i'm not sure which line is causing the error but, i belive that line will throw a NullPointerException because the type of 'code' is 'int', not 'Integer'.
     
  5. Offline

    timtower Moderator Moderator

    Could you also mark the error line?
     
  6. Offline

    mAndAle

    The error line is int code, that is how KarimAKL says

    sorry for the reply so late, but there is no autobox?
    i mean that it converts the int to integer right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 2, 2020
  7. Offline

    KarimAKL

    @mAndAle The problem is that the returned 'Integer' is null, which then throws an error when you declare it as an 'int'.
     

Share This Page