(JDK 1.6.0_23, Eclipse 3.7.0 with "Potential null pointer access" warning level at "Warning") Consider the following example code:
Object obj = null;
for (;;) {
obj = getObject();
if (obj != null) break;
Thread.sleep(25);
}
obj.toString();
I'm getting the following warning on the last line: Potential null pointer access: The variable obj may be null at this location. Is there any real way for obj to actually be null or why the compiler thinks so?
I'd say the compiler sees this as:
Object obj = null;
[Too dumb to interpret this loop]
obj.toString();
So obj.toString() could be null if you cannot interpret what the loop does.
For example you can trick the compiler by replacing:
void x(int x){
return;
x++; //error unreachable code
}
with
void x(int x){
if(true) return;
x++; //compiles
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With