Solved HashMap throwing NullPointerExceptions

Discussion in 'Spigot Plugin Development' started by brandonlea, Jan 13, 2020.

  1. Hello, I am trying to store data into a hashmap when a command has been executed when I try and get the data from the hashmap inside of an event it makes the hashmap null any help would be great thanks.

    Code (Java):
      Player target = Bukkit.getPlayerExact(args[0]);

                        if(target != null) {
                            arenas.addArenas(player, arenas);
                            duel.getPlayerMap().put(player, target.getName());
                            duel.getIngame().put(player, true);
                            player.sendMessage(duel.getPlayerMap().get(player));
                        } else {
                            player.sendMessage("Please select a valid person!");
                        }

    Code (Java):
     //Bets
            if(inventory.getName().equalsIgnoreCase(bets.getName())) {
                if(itemStack.getType() != null) {
                    if(itemStack.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + ChatColor.stripColor(itemStack.getItemMeta().getDisplayName()))) {
                        player.sendMessage(duel.getPlayerMap().get(player));
                        player.closeInventory();
                    } else if(itemStack.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + ChatColor.stripColor(itemStack.getItemMeta().getDisplayName()))) {
                        player.closeInventory();
                        player.sendMessage("Sorry you don't have enough money");
                    }

                }
            }//end of Bets

    Code (Java):
    private HashMap<Player, String> playerMap = new HashMap<>();
     
  2. sounds like either you're trying to pull something that doesn't exist or you don't make sure is not null before doing something with the value
     
  3. Nope, I ran two types of instances.