0

We're having trouble tracking down Null Pointer Exceptions in our code using Eclipse and I was wondering if Eclipse offered any help in this area. What ways does Eclipse offer to easily track down Null Pointer Exceptions?

To illustrate, here is a typical code example that is throwing an NPE:

if (myObject.getDimensionMap().get(keyValue.getConcept()).isMeasureDimension()) {

Unless I'm mistaken there are 5 possibilities where an object could be null:

  • myObject could be null
  • myObject.getDimensionMap could be null
  • keyValue could be null
  • keyValue.getConcept could be null
  • myObject.getDimensionMap().get(keyValue.getConcept) could be null

Avoiding the argument of changing the code so each statement is on a different line, does Eclipse offer any way of helping to find the NPE without having to step into each statement? Bear in mind that it took the dev team quite a while to even get to this statement let alone find out which one of the elements was null (it was the getDimensionMap().get ).

Update:

It took so long to find the actual cause of the NPE because the offending code was buried in a deep call hierarchy, where the outer layer has a catch for all exceptions. The reason for this is so that the exceptions can then be rendered in a more attractive format and displayed to the user. So the code is like

try {
  doSomething
  ...
    callSomething
      callSomethingElse
      ...
      offending if test which throws the NPE
      ...
} catch (Exception e) {
   // Format exception and pass back to client
}

Of course, the user never want to see the phrase NullPointerException....

Thanks.

Phil
  • 1,897
  • 4
  • 25
  • 48
  • 1
    Is the question really: "Apart from fixing the code or debugging it correctly, what other ways does Eclipse offer me to find the bug?" Because if it is, the answer is: none. Even an exception breakpoint will only take you to the line because line is the basic unit of debugging defined in Java. – biziclop Jul 01 '15 at 09:39
  • It would help if you could tell us why it took them so long to find what was null. In the average case it should be really straightforward: set exception breakpoint, when program stops, check variables, if everything's fine, step into first method, check variables, step into next method, check variables... – biziclop Jul 01 '15 at 09:48
  • OK, I'll update the question with why it took so long. I was hoping for a feature in Eclipse such as "break on the line when a system exception is thrown" – Phil Jul 01 '15 at 10:04
  • @biziclop Ah! After re-reading I see that you mentioned the exception breakpoint too, so I upvoted you. Thanks - we weren't aware of this feature. – Phil Jul 01 '15 at 10:21
  • Cheers, that was the missing piece of the puzzle. :) – biziclop Jul 01 '15 at 10:34

1 Answers1

4

From Eclipse, Run/Add java Exception Breakpoint.

Select NullPointerException. Ensure 'suspend on caught exception' is ticked.

Only downside is it will trigger on any code that intentionally raises and catches NPEs internally.

soru
  • 5,464
  • 26
  • 30
  • Perfect and thanks for the warning about internal NPEs. There is also a very clear explanation of how to do this here: http://stackoverflow.com/questions/3066199/eclipse-break-when-exception-is-thrown – Phil Jul 01 '15 at 10:16
  • To avoid stopping on unrelated exceptions (typically thrown during startup of certain libraries), you can set a regular breakpoint as close to the problematic code as possible, and only set the exception breakpoint once execution has reached that point. – biziclop Jul 01 '15 at 10:33