Solved Why hashmap return null?

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

  1. Offline


    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

    private HashMap<UUID, Integer> codeMap = new HashMap<UUID, Integer>();
        private HashMap<Integer, UUID> uuidMap = new HashMap<Integer, UUID>();
            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())) {
                         } 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 {
                         return true;
                } else {
                    return true;
                return false;
    event code:

            public void onQuit(PlayerQuitEvent e) {
                int code = this.codeMap.get(e.getPlayer().getUniqueId());


    org.bukkit.event.EventException: null
        at$1.execute( ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerList.disconnect( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.NetworkManager.handleDisconnection( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.ServerConnection.c( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C( [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
        at Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at org.mAndAle.Tokens.discord.BotCreator.onQuit( ~[?:?]
        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$1.execute( ~[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


    sorry i'll post the error
  4. Offline


    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


    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


    @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