In this case, Infer is correct: the method test() by itself is harmless, because it could be the case that "s" has been initialised before it is called.
Infer does a bottom-up analysis (callees before callers), and will infer that test() expects "s" to be allocated to run correctly.
To get an actual error, you need to call test() without initialising s, or with s being set to null by some other means prior to the call.
Here is a modified version of your example that will get Infer to complain.
class Hello { private String s;
int test() { return s.length(); }
int foo() { Hello a = new Hello(); return a.test(); }
int bar() { Hello a = new Hello(); a.s = null; return a.test(); }
}
Running "infer -- javac Hello.java" will show an error in bar(). Infer also finds the error in foo() but doesn't report it as it considers it lower-probability. This is a trade-off made in Infer to try and report only high-probability bugs. In this case it could be improved. To see that Infer finds the error in foo(), run "infer --no-filtering -- javac Hello.java".
(Infer dev here)
One strength of Infer is that it is inter-procedural, yet not whole-program: each procedure gets analyzed independently. So it's cheap enough to run on large codebases while still able to find deep inter-procedural bugs.
Infer does a bottom-up analysis (callees before callers), and will infer that test() expects "s" to be allocated to run correctly.
To get an actual error, you need to call test() without initialising s, or with s being set to null by some other means prior to the call.
Here is a modified version of your example that will get Infer to complain.
class Hello { private String s; int test() { return s.length(); } int foo() { Hello a = new Hello(); return a.test(); } int bar() { Hello a = new Hello(); a.s = null; return a.test(); } }
Running "infer -- javac Hello.java" will show an error in bar(). Infer also finds the error in foo() but doesn't report it as it considers it lower-probability. This is a trade-off made in Infer to try and report only high-probability bugs. In this case it could be improved. To see that Infer finds the error in foo(), run "infer --no-filtering -- javac Hello.java".