This snippet throws an NullPointerException
due to the fact that its unboxed to a primitive type and Long.longValue()
is called, right?
Thats even easy to see if you have a snippet like this:
long value = (Long) null;
But the NullPointerException
is even harder to get in a more complex situation like this:
long propertyValue = (Long) obj.getProperty(propertyModel.getName());
So isn't there any possibility for the Java-Compiler to make a more comfortable Exception out of this? I would prefer an IllegalArgumentException
with a message like "You're trying to cast a null-Object into a primitive type, this can't be done!"
Wouldn't this be more appropriate? What do you think? Is this even possible at runtime? Are we able to determine this cast? I haven't yet looked at the java bytecode. Maybe it could be used in a solution.
This question can be answered: I'd like to know if it's possible to achieve this behaviour!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…