Retrieve key with value in a HashMap, NPE

Discussion in 'Plugin Development' started by PumpMelon, Jan 1, 2017.

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


    Hello guys,

    I'm trying to randomly loop through my HashMap, get a value, then get the key from said value. Though there doesn't seem to be a way to do this. So I've tried comparing each Key to the value and see if it maps up. Though my logic may just be wrong on this one, but I have a NPE on what I'd think shouldn't be null.

    Here it is in pastebin

    import java.util.Random;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import net.md_5.bungee.api.ChatColor;
    public class NewPackageCMD implements CommandExecutor {
        public boolean onCommand(CommandSender s, Command arg1, String arg2, String[] args) {
            if(s instanceof Player){
                Player p = (Player) s;
                //They are not a postman, don't let them continue
                if(Jobs.getJobForPlayer(p) != "postman"){
                    p.sendMessage(ChatColor.RED + "This command is for Postmen only!");
                    return true;
                    for(String loc : Jobs.locations.keySet()){
                        //This would be the potential package
                        ItemStack potentialPackage = ItemUtils.createItem(Material.PAPER,ChatColor.YELLOW + "Drop off at the " + loc.toLowerCase(), ChatColor.GRAY+
                                "Go to the " + loc.toLowerCase() + " with this, and type /dropoff");
                        //They already have a package, don't give them a new one.
                            p.sendMessage(ChatColor.RED + "You already have a package to deliver!");
                            //Randomly select a location from my HashMap locations that stores a <String, Location>
                            Location newLoc = Jobs.locations.get(new Random().nextInt(Jobs.locations.size()));
                            //This is the part that I think is causing the trouble, 
                            //I tried comparing all the locations in the HashMap to see if they were equal to the new one
                            Location oldLoc = Jobs.locations.get(loc);
                       //This is where the NPE is     if(Jobs.locations.get(loc).getX() == newLoc.getX() && oldLoc.getY() == newLoc.getY() && oldLoc.getZ() == newLoc.getZ()){
                                p.sendMessage(ChatColor.RED + "Oh no, something has gone wrong and we couldn't find a new package for you!");
            return true;
    Thanks for the help,
  2. Offline


    Instead of looping through all the values, and trying to find the key that matches, why not loop through the entries? Entries contain both a key and the linked value. Use HashMap#entrySet() to get the entry set.
Thread Status:
Not open for further replies.

Share This Page