What do you do when you are debugging in code you did not write, and Eclipse goes mad?

I happened in a weird situation where I was debugging my project and I had a NullPointerException from one of the libraries I used.

The weird part was that even though I had recompiled the library and regularly attached the sources in Eclipse project, the debugged would not catch the right lines it was executing, but just showing some other lines from the source code.

Now I’m personally not an estimator of the NullPointerException. I believe that the JVM should at least put in the exception message the name of the variable that was found null.

The situation wasn’t too nice, because the library was a complex one. So I didn’t want to touch it too much by adding too much code to it, but at the same time, Eclipse would not help so I had to do something.

After thinking a bit I realized that really I could exploit the NullPointerException itself as a runtime exception to investigate the problem. NullPointerException is thrown when you try to use an object, but the variable you use to reference it is null.

For example:

String something = null;

if (something.equals("nono")) {
// the line above throws a NullPointerException
}

I loved this solution because this is really transforming your problem in a solution. Let me tell you something about runtime exceptions.

A runtime exception is one exception that you don’t need to explicitly catch in your programs. It’s like a ghost that can climb up your runtime stack and make you get crazy to debug.

So, how I exploited the NullPointerException? I just filled the source code of the library (just in the area where I got the problem) with if clauses like this:

if (inst == null) {
throw new NullPointerException("inst was null");
}

Basically I was manually checking which of my objects were null and throwing the NullPointerException writing in the message the name of the variable. This helped me in understanding where the problem was. and since NullPointerException is a runtime exception and I don’t need to explicitly throw runtime exceptions. I didn’t have to change any of the method signatures.

For once the problem helped to find its solution.

Similar Posts:

Bookmark and Share

Category: General Comment »


Leave a Reply



Back to top