Debugging

Hi forum,

One of those ruddy god-forsaken NullPointerExceptions popped up in my code. I have been trying to fix it for hours, but I cant find the problem. I was making such good progress too! :open_mouth: I hate the thing, and I want to perform an exorcism on my computer to get it out.

Legend has it that you can use a debugger to exhume a demon from your code. Spirit shamans tell me that the SDK has one built-in. I have motored on without one thus far, but this exception is tricky, and I need to learn to use one eventually, right?

So can anyone maybe tell me what a break point is, how to access the debugger, and perhaps whether to drive my wooden stake into my CPU or hard drive? I would be grateful for some pointers (pun intended(please dont make it NULL :D))

:wink: Thanks forum

Well first step is to find what variable is null. So put a break point , Ctrl + F8 on the line with the NPE (theres also other ways to put breakpoints down). Then click the “Run debugger” icon or Ctrl + F5, then when that line of code is hit, the execution will pause and you can look at the values of the variables. You can go up the call stack, recall functions, step through code (as long as there isn’t an error), watch variables, add condition expressions, etc…

1 Like

NPEs are pretty easy with some experience. They are generally the single easiest exception to track down.



For example, I bet if you posted the code then you’d have 50 people pointing to your error. :slight_smile: Some might even offer you non-debugger strategies for tracking it down next time… since usually simply looking at the code is enough.

2 Likes
@pspeed said:
NPEs are pretty easy with some experience. They are generally the single easiest exception to track down.

For example, I bet if you posted the code then you'd have 50 people pointing to your error. :) Some might even offer you non-debugger strategies for tracking it down next time... since usually simply looking at the code is enough.


I'd say ArrayIndexOutOfBoundsException is pretty easy to debug...
@javagame said:
I'd say ArrayIndexOutOfBoundsException is pretty easy to debug...


Except usually they come three layers down in some library code... and an index problem is nearly always a bug in logic far away or in unrelated code. Most NPEs come right near the problem.... and 9 times out of 10 it's an initialization problem or a poorly checked parameter.
1 Like