3

I am looking into using FindBugs to help find obvious bugs in my code.

I understand that any tool is limited in some ways, but the two most prevalent bugs I want to look for are:

  • NullPointerExceptions; and
  • Java memory leaks

Does FindBugs have the capabilities to find these? I'm sure one is never guaranteed to have all bugs uncovered, but it would be nice if FindBugs could detect even a fraction of them right there inside of Eclipse for me.

I have been reading the HTML manual and haven't come across anything that mentions these capabilities.

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

3

FindBugs uses static analysis to look for bugs in Java code. You could get good tips about improving your code, but it cannot guarantee you will detect memory leaks or NullPointerExceptions due to the runtime nature of both kind of problems. Certainly you will get very good tips about avoiding bad practices that could cause memory leaks and null pointers, but it is just a starting point.

Also check this question about finding memory leaks in Java.

The following Findbugs checks helps avoiding NullPointerException:

  • NP: Method with Boolean return type returns explicit null
  • NP: Clone method may return null
  • NP: equals() method does not check for null argument
  • NP: toString method may return null
  • NP: Null pointer dereference
  • NP: Null pointer dereference in method on exception path
  • NP: Method does not check for null argument
  • NP: close() invoked on a value that is always null
  • NP: Null value is guaranteed to be dereferenced
  • NP: Value is null and guaranteed to be dereferenced on exception path
  • NP: Method call passes null to a nonnull parameter
  • NP: Method may return null, but is declared @NonNull
  • NP: A known null value is checked to see if it is an instance of a type
  • NP: Possible null pointer dereference
  • NP: Possible null pointer dereference in method on exception path
  • NP: Method call passes null for nonnull parameter
  • NP: Method call passes null for nonnull parameter
  • NP: Non-virtual method call passes null for nonnull parameter
  • NP: Store of null value into field annotated NonNull
  • NP: Read of unwritten field
  • NP: Dereference of the result of readLine() without nullcheck
  • NP: Immediate dereference of the result of readLine()
  • NP: Load of known null value
  • NP: Possible null pointer dereference due to return value of called method
  • NP: Possible null pointer dereference on branch that might be infeasible
  • NP: Parameter must be nonnull but is marked as nullable
  • NP: Read of unwritten public or protected field

Some checks about memory issues that will help improve your code are

  • Dm: Method invokes inefficient new String(String) constructor
  • Dm: Method invokes inefficient new String() constructor
  • Dm: Method invokes inefficient Boolean constructor; use Boolean.valueOf(...) instead
Community
  • 1
  • 1
JuanZe
  • 8,007
  • 44
  • 58
  • FindBugs is not the best tool to do memory leak detection in Java. Any static analysis tools couldn't help much on this kind of issue. You should look at profiling tools and tools that shows runtime memory use and object graphs in order to find suspicious behaviour that could be a memory leak. – JuanZe Jan 11 '12 at 21:26
1

FindBugs is quite good at finding problems with nulls. http://findbugs.sourceforge.net/bugDescriptions.html describes many bugs FindBugs can recognize to do with nulls.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413