Q:

Why NullPointerException occur in short IF

I written short Java code which cause NullPointerException. Does anybody have explanation for this? Code:
int val = 2;
Boolean result = (val == 0) ? false : ((val == 1) ? true : null);

Also following (simplified version) code will cause NullPointerException:
Object result = (false) ? false : (false ? true : null);

But this:
int val = 2;
Boolean result = (val == 0) ? Boolean.FALSE : ((val == 1) ? true : null);

and this:
Object result = (false) ? Boolean.FALSE : (false ? true : null);

or this:
Object result = (false) ? (Boolean)false : (false ? true: null);

doesn't?

A:

I think what's happening is that ((val == 1) ? true : null) always returns null and it then tries to unbox that into a boolean.  That causes a null pointer exception.
After I said this, @JonSkeet marked your question as a duplicate because of NullPointerException in ternary expression with null Long  The answer there has a much more detailed explanation.  

