Community
Participate
Working Groups
Build ID: M20080221-1800 Steps To Reproduce: 1.Create a Java project 2.Create a .java source file 3.Enter a function like this: class Test { ArrayList params; public int getSqlParamCount() { return params == null ? null:params.size(); } } More information: If we remove the conditional, then the compiler complains: class Test { ArrayList params; public int getSqlParamCount() { return null; } }
Are you compiling in 1.5 mode ? What are your compiler settings? In 1.5, I think autoboxing is used in this case. It leads to a NPE at runtime.
javac 1.6 and 1.5.0 also compile this code.
That's true, I'm using 1.6 target. Even though, javac behaves so, I would consider this behavior as a bug.
The conditional operator offers special type checking rules, which allow for a boxing conversion to occur for free. The plain 'return null' is not as sophisticated. This is a situation where we stick to the language spec, and you should blame the spec (and then we would happily tune our semantics accordingly if you win this fight).
Added AutoboxingTest#test159
Thanks for the explanation. Luckily this behaviour isn't so important, so I'm not going to fight against anyone for this.