Possible Duplicate:
Java conditional operator ?: result type
NullPointerException through auto-boxing-behavior of Java ternary operator
Say I have two functions:
f(MyObject o) { ... }
f(int i) { ... }
And I call them like this:
f(someCondition ? 10 : null);
This compiles, but when I run it I get a null pointer exception (sorry I'm not sure on which condition). Some my questions are:
- Why does it even compile? What is the type of
foo ? 10 : null
? - It clearly doesn't call the "correct" functions because that wouldn't cause an NPE. So which function is it calling? Does it do
f((MyObject)10);
orf((int)null)
?