Discover NullPointerException bugs using FindBugs
When I run FindBugs on this code, it reports NO issues.
boolean _closed = false;
public void m1(@Nullable String text) {
if(_closed)
return;
System.out.println(text.toLowerCase());
}
While here it finds issue as expected:
public v开发者_高级运维oid m1(@Nullable String text) {
System.out.println(text.toLowerCase()); // FindBugs: text must be nonnull but is marked as nullable
}
Why does it fail in first case?
I agree with alex2k8. It is probably because of the _closed data member. Its initialization is irrelevant as long as it is not declared as final. Static analysis has no generic means for determining the actual values of _closed at runtime, and no software can ever do it (it is equivalent to the Halting problem).
I took FindBugs sources and searched for
NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE
Found two files:
- BuildUnconditionalParamDerefDatabase.java
- InconsistentAnnotations.java
Both consider "unconditional params dereferencing" only.
Looks like FindBugs is NOT so useful to find null-pointer issues :-(
P.S.
public void m1(@CheckForNull String text) {
if(_closed) // FindBugs: text must be nonnull but is marked as nullable
System.out.println(text.toUpperCase());
else
System.out.println(text.toLowerCase());
}
You want to use @CheckForNull instead of @Nullable
@Nullable only on those parameters, methods or fields that you want to allow to be null.
Seems like you are allowing null values for the text variable. You probably should use @NonNull instead.
Edit
I tried this out for my self and got the same result.
The text in the findbugs error (from the second method that does give the nullpointer bug) report says:
This parameter is always used in a way that requires it to be nonnull, but the parameter is explicitly annotated as being Nullable. Either the use of the parameter or the annotation is wrong.
My guess would be that since it is not a final Parameter FindBugs cannot/will not make any assumptions about the value of _closed since you can change it at a later time. I did even try to make the _closed variabel method scoped inside the m1-method, and it still doesnt report it as a bug.
edu.umd.cs.findbugs.annotations.Nullable [Target] Field, Method, Parameter
The annotated element could be null under some circumstances. In general, this means developers will have to read the documentation to determine when a null value is acceptable and whether it is neccessary to check for a null value. FindBugs will treat the annotated items as though they had no annotation.
In pratice this annotation is useful only for overriding an overarching NonNull annotation.
http://findbugs.sourceforge.net/manual/annotations.html
Run JavaLint - I suspect it will tell you that
System.out.println(text.toLowerCase());
in the first example is unreachable. Since it is unreachable, I'm guessing FindBug doesn't care that it could cause a NullPointerException
精彩评论