Java HashMap get method null pointer exception
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.
Related videos on Youtube
![nabster](https://i.stack.imgur.com/PnuIz.png?s=256&g=1)
nabster
When I am not building things with computers, I am building things with hands (aspiring DIY-er).
Updated on April 06, 2021Comments
-
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 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 over 11 yearsI 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 over 11 yearsThis is the correct answer. Also,
HashMap myMap<Character, Boolean>
is a syntax error (should beHashMap<Character, Boolean> myMap
), and you can't name methodsdo
. -
durron597 over 11 yearsGood answer, +1. Do you happen to know if
containsKey(c)
is faster thanget(c) != null
? -
Fritz over 11 years@durron597 Based on the implementation of both
containsKey
andget
, actually there is a difference in performance if the key is null.containsKey
calls the methodgetEntry
who iterates over the entry set, asget
does too (they're actually the same method, exceptget
does a null check with the key and returns null if the key is null, whilegetEntry
assigns a hash value of 0 if the submitted key isnull
, this particular case makes the difference).