This can happen when you cast a null to a primitive type. It can be hard to spot these types of errors:
Main.java:
import java.util.*;
public class Main
{
public static void main(String []args) {
// allow only integers to be added to the arraylist
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("1", 10);
map.put("2", null);
int a = map.get("1"); // works fine
int b = map.get("2"); // throws NullPointerException
}
}
The reason for the NullPointerException is much clearer if you look
at the equivalent:
NullPointerException when auto-unboxing
Joris Van den BogaertThis can happen when you cast a null to a primitive type. It can be hard to spot these types of errors:
Main.java:
The reason for the NullPointerException is much clearer if you look
at the equivalent: