A good way to debug nullPointerException [closed]

2019-01-17 01:59发布

问题:

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.

回答1:

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:

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

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:

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.



回答2:

  1. While debugging can you use the BreakPoints view, to capture the null pointers.

    Window -> Show View -> Breakpoints

    In the view there is a "J!" that lets you set breakpoints on exceptions. You can set java.lang.NullPointerException. So once null pointer exception is thrown, you can check which variable is causing null.

  2. Other thing you can do is capture possible null pointer access while you are coding. Off-course you cant capture all the Null Pointers using this setting. But still its helpful, set the following settings in your eclipse.

    Preferences -> Java -> Compiler -> Errors/warnings -> Null analysis



回答3:

There are a few ways to make NullPointerException less of an issue:

  1. Use the @Nullable annotation.

  2. Test the values of arguments for null in constructors and setters (avoid setters in plain old Java code). If you do this a lot you could make an (Eclipse) source code template to quickly generate the code. This is called "failfast" - you don't wait for the exception to be thrown when the value is being used further in the code, e.g. after storing it in a field.

  3. Make sure that you perform a minimum amount (e.g. 1 or 2) of operations per line. If you don't you will have to find out where in the single line expression the NullPointerException occurred.

  4. Don't use null in fields to indicate state. Instead, use e.g. an enum State. E.g. don't do if (socket == null) { // not connected } as it easy to forget to check for null later on. A explicit state is not so easy to forget.

  5. Don't initialize local variables to null unless explicitly needed. Actually, if you handle your scope and exceptions correctly you should be able to mark most variables final. Replacing the default printStacktrace() methods with throw new IllegalStateException("Unhandled program state", e) helps because it is seen as an exit point of a code block.

After a lot of programming you will find the number of NullPointerExceptions go down.