By Markus Sprunck; Revision: 1.3; Status: final; Last Content Change: Apr 25, 2013;
This tutorial describes selected warnings of Findbugs with Java sample code. Here JSR305 related (@Nonnull and @CheckReturnValue) warnings of FindBugs™ are in the center of attention. The following 3 warnings are covered in this article:
For static code analysis tools it is sometimes difficult to correctly detect not null usage. With JSR305 the developer can help Findbugs in the analysis. I strongly recommend to check each possible NullPointerException, but sometimes it is a good idea not to suppress each unexpected behavior.
Nathaniel Ayewah and William Pugh  said:
"Mistakes that manifest themselves by throwing exceptions are generally preferable to manifestations that silently and occasionally generate corrupted data or performance bottlenecks."
The use of @Nonnull and @CheckReturnValue annotations from JSR305 help to express the needs for null and return value checks. It is a good practice that the developer describes the expected behavior of the implementation for the later use and the static code analysis.
Motivation for the 'Findbugs™ Warnings By Example' Series
Unfortunately, some parts of the descriptions are not easy to understand (even for experienced Java developers). It can be difficult to understand the exact root cause for a warning and/or to find the correct way of fixing.
To be honest - at least I had problems with some warnings in the last years. Quite often, I found no helping sample code in the web. The main weakness of the bug descriptions is, that it uses seldom sample code to demonstrate the wrong and correct situation.
Findbugs™ (Version 2.0.1-rc2) will show all warnings with the minimum rank 13 (of concern) and confidence Low of the Findbugs™ Eclipse Plugin (Version 188.8.131.5220511). The code has been compiled with JDK 1.6.0_24. You will need the jsr305.jar (Findbugs lib directory) file in the class path.
The program should print the following to the standard output:
Please, do not hesitate to contact me if you have any ideas for improvement and/or you find a bug in the sample code.