'Hashmap get returns null

I have two Hashmaps

HashMap<Integer, Integer> inventoryRequirements = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> inventory = new HashMap<Integer, Integer>();

I have a loop that checks current and adds to it:

for(Item item : items){
    int currentRequirement = 0;
    currentRequirement = inventoryRequirements.get(item.get_id());
    inventoryRequirements.put(item.get_id(), currentRequirement++);         
}

I have another loop that checks inventory and adds to it:

for(Item item : items){
    int currentInventory = 0;
    // this next line returns null
    currentInventory = inventory.get(item.get_id());
    inventory.put(item.get_id(), currentInventory++);           
}

The first one works fine, but the second one pukes and returns a null. I don't understand why the second one does not work. Both are initially in the same state as depicted in the code sample.

EDIT

The two HM get populated as you see here - really! I know it may be hard to believe but the first one works and the second one does not.



Solution 1:[1]

Both of the loops you show will throw a NullPointerException (NPE) if the key you request via get() is not in the Map.

Map.get() returns null when the key is not present (or of course if the value stored with that key is null). An Integer (autoboxed type) that is null can not be autounboxed into an int so it throws an NPE.

The safe way of performing this is:

for (Item item : items) {
    Integer currentRequirement = inventoryRequirements.get(item.get_id());
    if (currentRequirement != null) {
        inventoryRequirements.put(item.get_id(), currentRequirement++);         
    }
}

Of course, it's also completely possible that you have an Item in your collection that is null and that is what is throwing the NPE.

Solution 2:[2]

If the code you have provided is complete, then you haven't put anything in your hashmap. So it will always return null.

Solution 3:[3]

You are getting a item from inventory but it's empty

Solution 4:[4]

The best practice is to write the code in the following manner:

for(Item item : items){
int currentRequirement = 0;
currentRequirement = inventoryRequirements.get(item.get_id());
if(currentRequirement!=null){
//update currentRequirement only if it exists in the map.
   inventoryRequirements.put(item.get_id(), currentRequirement++); 
} else {
   //add it to the map otherwise.
   inventoryRequirements.put(item.get_id(), 1);       
}

Solution 5:[5]

Ugg. Also, when all else fails, don't overlook the possibility that it is not returning null because its not finding the key. It could be returning null because when the key/value pair was added... the value added was null. Just spent more time than I care to admit tracking this down.

So, to recap, there are at least two reasons the HashMap could be returning null:

  • It's not finding the key
  • It is finding the key, but the value inserted for the key is null

Doh!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Prasad Kharkar
Solution 3 lleon
Solution 4
Solution 5 John