Avoid nullpointer exception with ConcurrentHashMap

23,965

Solution 1

It's the key that is null, which in this case is personAddress (as everyone is saying). However, it would be safest to check them all:

public static String getPersonInfo(String personAddress) {
  if (personAddress != null && concMap != null && concMap.containsKey(personAddress)) {       
    return concMap.get(personAddress);
  }
  return null;
}

I should add that the ability for almost everything but primitives to be null is a common headache in Java. Most internal methods don't even bother dealing with it and just throw the NPE. If something can't be null down the road, check for this at your first meaningful opportunity..

Solution 2

This is not possible. The API states, that this method only throws a NPE, if the key is null. You should use the debugger, somewhere your null-check is not performed. I also don't understand, why you posted the code with comments. What does that mean? Is that your null check? Why is it commented out?

Share:
23,965
rickygrimes
Author by

rickygrimes

Updated on September 11, 2020

Comments

  • rickygrimes
    rickygrimes over 3 years

    I know this question has been asked several times, but I didn't find any right answer for the question. How do you avoid NullPointerException from being thrown when you fetch results from a ConcurrentHashMap. I have the below code that is throwing me a NullPointer.

    public static String getPersonInfo(String personAddress) {
        //if(concMap!=null && concMap.containsKey(personAddress))       
        return concMap.get(personAddress);  
                //return null;  
    }
    

    In the concurrent hashmap,I am storing person id and address in a concurrent hashmap. In a few cases, the id is null. Although I do a null check before putting the data in the map, I still get the below error while trying to read the data.

    java.lang.NullPointerException
    at java.util.concurrent.ConcurrentHashMap.get(Unknown Source)
    

    Someone please help :( I have wasted over 3 hours trying to debug this. Although I see a lot of posts online, I am not very clear yet with what can be done to avoid it.

  • rickygrimes
    rickygrimes over 10 years
    I commented it out since it wasn't helping. Yes, the key in null in a few cases, and in a few cases, the value is null.
  • weiglt
    weiglt over 10 years
    OK, the problem is that you checked concMap for null, but you should check personAddress for null ;) Because personAddress is the key and the API states, that NPE is thrown, when the key is null. The value is ok to be null. - Basically you can ofcourse also check concMap for null, but that's only if it can be null.
  • rickygrimes
    rickygrimes over 10 years
    @weigit - Sure, I'll do that. But I just checked its failing when the value for a given key is null. How do I avoid this?
  • weiglt
    weiglt over 10 years
    Edit: Sorry, got you wrong. Well, the value should be ok to be null. It's just returning null then.
  • weiglt
    weiglt over 10 years
    I agree, that's probably the safest way. But it's also possible to live with the exception and handle it...
  • wwwslinger
    wwwslinger over 10 years
    Absolutely. Don't bother calling getPersonInfo() if personAddress is null, and deal with exceptions otherwise.
  • rickygrimes
    rickygrimes over 10 years
    Perfect. Doing a null check on the key worked. Thanks so much for your inputs everyone :)
  • weiglt
    weiglt over 10 years
    Glad it works now. Sometimes it's better to ask, than to waste hours of time on your own ;)