I have two variables:
final Long a;
if(...) {
a = ...;
} else
a = null;
String b = "A";
....
and then later, assignment to another Long is throwing a NullPointerException:
final Long c = b.equals("B")? a*1000: a;
I am expecting c to be null if a is null, and I’m expecting b, which I know to not be B in these circumstances to prevent it being dereferenced.
So why does it throw the NullPointerException?
UPDATE: mystery solved, its auto-unboxing in the tenary operator. See https://stackoverflow.com/a/13627200/15721
Aha, from finding code that works I can infer what is happening:
Because
a*1000is unboxinga, the tenary expression islongnotLong.Keeps the tenary outcome
Longand works as expected.