Nullpointer Exc - what var is causing it?

why on earth does java not tell you WHAT VARIABLE IS CAUSING NULLPOINTER EXCEPTION??? this really drives me nuts… it seems to me this would be a no-brainer…

I have a large no. of vars in my Tomcat webapp; how on earth am I supposed to find out what var is causing nullpointer exc if all error prints is

java.lang.NullPointerException

it doesn’t even tell you what file, what line, is throwing error… oh brother…

is there any way I can find out what var is causing this nullpointer exception?

thank you very much…

When you write a try/catch, what do you put where I’ve placed the red text?:


try {
  // some code
} catch( Exception e ) {
[COLOR="Red"]  // Exception handling code[/COLOR]
}

so what do I put in Exception-handling code that would tell me what variable is causing the nullPointer Exception?

thank you…

hi maya90,
try this:


e.printStackTrace();

I think I know the answer, but didn’t want to accuse you of something you didn’t do.

At any rate, to answer your question: yes, very much so.

Java is set up to be very easy to troubleshoot, when you get an exception you’ll get a stack trace, which not only tells you what the problem is, but where it is and how the program came to be in that bit of code.

Unless, that is, you shoot yourself in the foot by trying to handle the printing of the error message yourself - and do it incorrectly.

I’ll guess that your exception handling code looks like this:

System.out.println( e.getMessage() );

Which will output only the error message, in this case: java.lang.NullPointerException.

What you want, at a minimum, is:

e.printStackTrace();

Which will print the trace to standard out, in the trace you will find the class and line number where the error occurred.

As it’s a web app, logging would be preferred. log4j is a good choice, but not the only choice.

tl;dr: This is a problem with your lackluster coding abilities, not Java.

some object that you want to access dont be initializing, try to put the stacktrace to help you to resolve your problem

thank you… e.printStackTrace() prints nothing, I get a completely blank page (this is on a JSP…) but: decided to try something, took out try-block from around the code… THEN it printed like a typical compiler error, and said exactly what variable was throwing error… in what file, what line etc… :wink:

thank you…