No. NullPointerException does not always have to be caused by a variable/identifier being null
. E.g. throw new NullPointerException()
and throw null
. Though it is often the case, it is not strictly required.
In most cases, it is fairly obvious what has caused the NPE. If not, then you may have too much going on in one line of code.
Consider this use case:
foo.doWork(bar1, bar2, bar3);
Here it is obvious that foo
is `null.
Another case:
foo.doWork(bar.get(), bar2.get())
Here it could be foo
, bar
, or bar2
.
The point is, that armed with this information and a breakpoint, it should be obvious what was null
. If worse comes to worse, a static code analyzer like FindBugs
could also give you some hints.