0
Integer a = true?null:0;

Works Fine

Integer b=null;
Integer a = true?b:null;

Works Fine

Integer b=null;
Integer a = true?b:0;

Runtime Null Pointer Exception

Works the same with Character but not with String or a normal Object

This looks like java is calling a method inside of the b which gives the exception but it is validating first if both ends of the single line if's are the same then it is not calling it and just performing the assign.

Either Way this seems like a bug, shouldn't be calling for a method (like intValue or something) instead it should do a normal assignation of the value correct?

Do you have any information or documentation on the matter?

White_King
  • 748
  • 7
  • 21
  • Thank you. Should I just delete? – White_King Mar 06 '19 at 00:35
  • You'll get your final answer in the question linked in this dupe question. I chose the other question (not the one where the real answer is), because you may want to understand first why the type of the ternary operator is important here. So the existing answer there might help you as well. – Tom Mar 06 '19 at 00:36
  • I just read it, and you are right, it does not answer the whole thing. – White_King Mar 06 '19 at 00:36
  • "Should I just delete?" That's up to you. When you think this question could be a useful sign post for other users with a similar question to be led into the correct direction, when you can keep this question. – Tom Mar 06 '19 at 00:37
  • 2
    Both of `Integer a = true ? 0 : b;` and `Integer a = true ? b : new Integer(0);` will work fine because of the JLS quote provided in the duplicate answer. Why do you think it doesn't answer the whole thing? – Kartik Mar 06 '19 at 00:42
  • @Kartik, They do not provide any source or documentation on the matter, it is just their word, the same thing I could have guessed by myself :), actually I did, in my question. – White_King Mar 13 '19 at 20:19
  • @White_King They have provided the Java Language Specification link [JLS 15.25](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25), and quoted what it says there (the yellow block is a quote, which means the answerer is quoting what is written in JLS). – Kartik Mar 13 '19 at 22:31

0 Answers0