NPE annotation scenarios and static-analysis tools for Java
Posted
by alex2k8
on Stack Overflow
See other posts from Stack Overflow
or by alex2k8
Published on 2010-05-08T17:11:33Z
Indexed on
2010/05/08
17:18 UTC
Read the original article
Hit count: 280
Here is a number of code snippets that can throw NullPointerException.
01:
public void m1(@Nullable String text) {
System.out.print(text.toLowerCase()); // <-- expect to be reported.
}
02:
private boolean _closed = false;
public void m1(@Nullable String text) {
if(_closed)
return;
System.out.print(text.toLowerCase()); // <-- expect to be reported.
}
03:
public void m1(@NotNull String text) {
System.out.print(text.toLowerCase());
}
public @Nullable String getText() {
return "Some text";
}
public void m2() {
m1(getText()); // <-- expect to be reported.
}
Different people have access to different static-analysis tools. It would be nice to collect information, what tools are able to detect and report the issues, and what are failing. Also, if you have your own scenarious, please, publish them.
Here my results
FindBugs (1.3.9):
- 01: Parameter must be nonnull but is marked as nullable
- 02: NOT reported
- 03: NOT reported
IntelliJ IDE 9.0.2 (Community edition):
- 01: Method invocation text.toLowerCase() may produce java.lang.NullPointerException
- 02: Method invocation text.toLowerCase() may produce java.lang.NullPointerException
- 03: Argument getText() might be null
© Stack Overflow or respective owner