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
- iftarget
orreplacement
isnull
.
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:
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 setjava.lang.NullPointerException
. So once null pointer exception is thrown, you can check which variable is causing null.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:
Use the @Nullable annotation.
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.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.Don't use
null
in fields to indicate state. Instead, use e.g. anenum State
. E.g. don't doif (socket == null) { // not connected }
as it easy to forget to check fornull
later on. A explicit state is not so easy to forget.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 variablesfinal
. Replacing the defaultprintStacktrace()
methods withthrow 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 NullPointerException
s go down.