The Java language definition includes a concept of automatic "boxing" and "unboxing"
This allows primitive type (boolean
, int
, double
etc) to be automatically converted to their Object
equivalents (boxing) and for objects like Boolean
, Integer
etc to be converted to primitives (unboxing)
But unboxing requires that the object (eg Boolean
) not be null. If it is null, then that is a runtime error (NullPointerException
). It must be a runtime error, because the language does not provide enough information to consistently detect null conditions at compile time.
In your case, the compiler is attempting to unbox your Boolean booleanFlag
into a boolean
so that it can be used for an if
condition, and that conversion is failing.