Why does the following line of code produce a NullPointerException?
Long v = 1 == 2 ? Long.MAX_VALUE : (Long) null;
I understa
From the JSL
- If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
- If one of the second and third operands is of type
booleanand the type of the other is of typeBoolean, then the type of the conditional expression isboolean.
In following statement the type of second operand is long and third is Long.
Long v = 1 == 2 ? Long.MAX_VALUE : (Long) null;
This will work if an expression is true.
Long v= 1 == 1 ? Long.MAX_VALUE : (Long) null;
Or you may cast it.
Long v= 1 == 2 ? Long.valueOf(Long.MAX_VALUE) : (Long) null;