While executing the following code, I am getting a NullPointerException
at line:
value = condition ? getDouble() : 1.0;
In earlier lines when I use null
instead of getDouble()
everything works and this is strange.
public class Test {
static Double getDouble() {
return null;
}
public static void main(String[] args) {
boolean condition = true;
Double value;
value = condition ? null : 1.0; //works fine
System.out.println(value); //prints null
value = condition ? getDouble() : 1.0; //throws NPE
System.out.println(value);
}
}
Can someone help me understand this behavior?