Java HashMap get method null pointer exception

112,548

Solution 1

If c is not contained in myMap, it will return null, which can't be unboxed as a boolean.

Try :

Boolean b = myMap.get(c);
if(b != null && b){
...

Solution 2

If myMap doesn't contain a key that matches c, then myMap.get(c) will return null. In that case, when the JVM unboxes what it expects to be a java.lang.Boolean object into a boolean primitive to execute the condition, it founds a null object and therefore throws a java.lang.NullPointerException.

The following block is equivalent to what you have in your example and should make it easier to understand why you would have a NullPointerException:

if (((Boolean) myMap.get(c)).booleanValue()) 

I would re-write your original condition as:

if ( myMap.containsKey(c) )

I hope this helps.

Solution 3

Change

if ( myMap.get(c) )

to

if ( myMap.containsKey(c) && myMap.get(c))

Solution 4

A stab in the dark: is there an entry in your map for the particular character assigned to c? If there isn't, Java may be trying to unbox a null value...

Solution 5

If there is no entity with required Character in map, then map.get(key) returns null and inside if statement it leads to NullPointerException throwing.

Share:
112,548

Related videos on Youtube

nabster
Author by

nabster

When I am not building things with computers, I am building things with hands (aspiring DIY-er).

Updated on April 06, 2021

Comments

  • nabster
    nabster about 3 years

    My code is similar to the following:

    public class A {
            private HashMap<Character, Boolean> myMap;
    
            public A() {
                myMap = new HashMap<Character, Boolean>();
                String mychars = "asdfzxcvqwer";
                for (char c : mychars.toCharArray())
                    myMap.put(c, true);
            }
    
            public void doo(String input) {
                StringBuilder output = new StringBuilder();
                for (char c : input.toCharArray()) {
                    if (myMap.get(c))
                        output.append(c);
                }
            }
            //...
            //...
        }
    

    Why am I getting a null pointer exception (NPE) at the line if (myMap.get(c))?

    • PermGenError
      PermGenError over 11 years
      @talnicolas check his map declaration and also do is a reserved keyword in java, you cant use it as a method name
    • Admin
      Admin over 11 years
      I typed out the code manually into SO -- apologies for both the errors. Thanks very much for the answers, everyone. I'll make the edits.
  • durron597
    durron597 over 11 years
    This is the correct answer. Also, HashMap myMap<Character, Boolean> is a syntax error (should be HashMap<Character, Boolean> myMap), and you can't name methods do.
  • durron597
    durron597 over 11 years
    Good answer, +1. Do you happen to know if containsKey(c) is faster than get(c) != null?
  • Fritz
    Fritz over 11 years
    @durron597 Based on the implementation of both containsKey and get, actually there is a difference in performance if the key is null. containsKey calls the method getEntry who iterates over the entry set, as get does too (they're actually the same method, except get does a null check with the key and returns null if the key is null, while getEntry assigns a hash value of 0 if the submitted key is null, this particular case makes the difference).