When you get an NPE and identified the line, the first thing you check is all occurrences of the dereferencing operator (.
). The value on the left hand side(!) of such an operator will be null
. (If you have multiple .
s on one line, then one of those. So if your line is foo.getBar().doSomething()
, either foo
is null
or foo.getBar()
returns null
. But what doSomething()
returns does NOT matter.)
In this case, it will probably be the getTable()
method that returns null
.
Since Java 5 there have been two new, rather more sneaky possible causes of NPE:
- Implicit dereferencing in
for (Foo foo : fooCollection)
, here you get an NPE iffooCollection
is null. - Auto-unboxing of numbers:
Integer i = null; int j = i + 1; //NPE here
That's all you need to know about the debugging of NPEs in general, there's of course the even more general advice that you should either log variable values or use a debugger to execute your code step-by-step.