Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A good way to debug nullPointerException [closed]

Tags:

I am using eclipse and programing in java. Sometimes I come across a nullPointerException that will plague me for hours. Is there anyway to debug nullPointerExceptions better and figure out what variable values are and other things that would cause null Pointer Exceptions.

like image 494
Noah Huppert Avatar asked Jul 26 '13 02:07

Noah Huppert


People also ask

How do you debug your application if you encounter a NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What can help us in avoiding NullPointerException?

Java 8 introduced an Optional class which is a nicer way to avoid NullPointerExceptions. You can use Optional to encapsulate the potential null values and pass or return it safely without worrying about the exception. Without Optional, when a method signature has return type of certain object.

Is it good practice to catch NullPointerException?

It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.


1 Answers

Look at the stack trace and read the line number where the NullPointerException is thrown. Almost always, the line has some method invocation like

x.getValue() 

If x is null, you get a NullPointerException. If the method at the top of the stack trace is not your code, trace the stack all the way back until you do reach your code. Very often in this case, you are passing null to a method that doesn't like null parameters. Find it and fix it.

Also, very often when you encounter a method that is not yours that is throwing a NullPointerException, read the documentation. For example, look at String.replace(CharSequence target, CharSequence replacement):

Throws:

NullPointerException - if target or replacement is null.

It doesn't get much clearer than that!

Here's an example:

enter image description here

Looking at line 4, we see foo.bar(). This implies that foo is null. That one is easy. Let's look at another example:

enter image description here

Tracing back to your code, we see that s.replace(target, replacement) is throwing. We should inspect target and replacement. Let's attach a debugger:

enter image description here

Aha! replacement is null. You can do the same thing in Eclipse. Set a breakpoint for when an exception is thrown, and use it to inspect the parameters to your method. I'm primitive here because I come from a school of thought where I genuinely believe everyone would be better off if they learned to do it the hard way first. Leaky abstractions and all that.

like image 151
jason Avatar answered Oct 14 '22 22:10

jason