(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?
The compiler is seeing this as a potential that the object will not be initialized. It is not really able to see that the loop breaking condition is that the object is not null or it will just run forever or until it is no longer null. I only get the warning on the JDK you mentioned. Using a more updated version this warning does not appear.