Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 void 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?

like image 328
alex2k8 Avatar asked May 07 '10 21:05

alex2k8


2 Answers

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).

like image 69
Eyal Schneider Avatar answered Oct 21 '22 05:10

Eyal Schneider


I took FindBugs sources and searched for

NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE

Found two files:

  1. BuildUnconditionalParamDerefDatabase.java
  2. 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());
}
like image 30
alex2k8 Avatar answered Oct 21 '22 04:10

alex2k8