Long n = null;
for (Long price : new Long[]{null, 2L, 10L}) {
n = (n != null) ? 0L : price;
}
I’m stumped, why am I getting a NPE when I execute this code ?
Seems to be just a simple assignment of n = price where price is null.
Don’t worry about what it means, it makes no sense.
In the line
n = (n != null) ? 0L : price;, you have alongand aLongas the alternatives to your?:statement. Java is going to construe this as of typelongand will try to unbox theLong, instead of box thelong. Whenpriceis null, as it is in the first iteration, this generates an NPE.