Re: nullpointer exception



maya wrote:

hi,

how do you find out what variable is causing a NullPointerException please.. (WHY can't that error tell you what variable is causing this error??)

(it says: The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs, but none of the log-files created today have any info on this error..)

HOW do you debug something like this if you don't what variable is causing this error? it's caused by a variable that evaluates to null, but all my variables are declared and initialized (it's a servlet, adapted from here, http://zetcode.com/tutorials/jeetutorials/sendingemail/)
all I did was change the relevant variables (password, etc..)

it compiles fine, but get NullPointer at runtime.. have no idea what variable is causing it..

If the stack trace shows you which source line threw the
exception, the null reference must be one of those mentioned
in that line -- this is usually a fairly small number of
possibilities, and if you concentrate on just those few you
can usually figure out which one is wrong. If not, you can
try decomposing the line:

// original
String s = foo.toString() + bar.toString()
+ child.getParent().toString();

// decomposed
String s_foo = foo.toString();
String s_bar = bar.toString();
Parent par = child.getParent();
String s_par = par.toString();
String s = s_foo + s_bar + s_par;

Since each line now uses only one reference, the line that
throws the exception highlights which reference is faulty.
You could also add assert statements if desired.

Keep in mind that the null reference might not be a
variable at all. For example, if you do

Map<String,Thing> map = ...;
...
map.get("findMe").doSomething();

.... it may be that map.get() returns null because the Map
contains no entry with the key "findMe", and then the
attempt to call null.doSomething() throws the exception.

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: Truss rod basic Q
    ... I have eyeballed the relief when I fret the low e string at the first ... tune up right after you make the adjustment (to bring it back up to the ... The truss rod is typically 8 or 10/32 thread, ... nut may save it from one day refusing to move smoothly, causing you to ...
    (alt.guitar.beginner)
  • Re: Programmatically determining selected index of combobox produces null error
    ... Which line is throwing the exception? ... You can check .SelectedIndex ... Something is causing a NullReferenceException ... Reference Chapters 13 and 11. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: [bug] hung bootup in various drivers, was: "2.6.21-rc5: known regressions"
    ... But I don't understand what is causing you to see this problem. ... is holding the reference on the struct device at this point in time? ...
    (Linux-Kernel)
  • Re: Strange Error
    ... The line that is causing me headaches is: ... My guess would be that your AuthorCode is longer than 256--I suggest you ... Visual Studio, but gives that error when compiled to an exe and run from ... wcscpy will just trample the end of the destination string over ...
    (microsoft.public.dotnet.languages.vc)
  • Re: String.valueOf() Memory Leak inside of thread.
    ... Thomas Hawtin wrote: ... This is causing a memory leak. ... If I change the line to read "String ... inside the loop but outside of the thread, ...
    (comp.lang.java.programmer)