Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: HashMap.getOrDefault returns null value

I'm fairly new to Java coming from a Python and C# background. I don't know why I am getting a null value for HashMap.getOrDefault() when from my understanding, this method is built in the first place to avoid NullPointer exceptions. The people object is not null, neither is idKey.

Screen Shot

like image 401
Benison Sam Avatar asked Mar 26 '21 08:03

Benison Sam


1 Answers

The getOrDefault() will return the default value when the key is not found, as per the documentation. If the key is found but the value is null, then null will be returned.

So, it seems that your map actually contains the key but the corresponding value is null.

like image 172
Tasos P. Avatar answered Oct 21 '22 20:10

Tasos P.