NullPointerException in Hashmap get method

I have NullPointerExceptionin this line:

 int qty = mSelectedBottles.get(bottleID);

I checked that mSelectedBottlesboth bottleIDare NOT NOT.

mSelectedBottleshas type Hashmap<Integer, Integer>and bottleIDhas typeint

+4
source share
1 answer

Because, Unboxing is null for a primitive data type. There is enough code to recreate this NullPointerExceptionyourself

int x;
HashMap<String,Integer> map = new HashMap<String,Integer>();
x = map.get("hello");

Auto-reporting non-existent value was a problem.

We know that to simplify the coding, autoboxing was introduced, but this is a certain anti-template performance and can lead to annoying errors like this, which are not intuitive.

autoboxing, , , , .

, mSelectedBottles.get(bottleID) null , int

Java

+10

Source: https://habr.com/ru/post/1534200/


All Articles