If a variable is dereferenced, and the variable may have a null value on some execution paths leading to the dereferencing, the dereferencing may result in a NullPointerException.

A variable may also be implicitly dereferenced if its type is a boxed primitive type, and the variable occurs in a context in which implicit unboxing occurs. Note that the conditional operator unboxes its second and third operands when one of them is a primitive type and the other is the corresponding boxed type.

Ensure that the variable does not have a null value when it is dereferenced.

In the following example, the use of the conditional operator causes implicit unboxing, since the integer literal has type int. If the parameter p is ever null then a NullPointerException will occur.

If the implicit unboxing is unintentional, it can be prevented by making sure that both branches of the conditional operator have the same type.

  • The Java Tutorials: Autoboxing and Unboxing.
  • Java Language Specification: Conditional Operator ? :.