Run the following Java code:
boolean b = false;
Double d1 = 0d;
Double d2 = null;
Double d = b ? d1.doubleValue() : d2;
Why is there a NullPointerException?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The return type of the conditional expression
b ? d1.doubleValue : d2isdouble. A conditional expression must have a single return type. Following the rules for binary numeric promotion,d2is autounboxed to adouble, which causes aNullPointerExceptionwhend2 == null.From the language spec, section §15.25: