• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tracing the NullPointerException  RSS feed

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which is the best way to trace out the nullpointerexception in java specially in jsp servlets/???
I use System.out.println(ClassNmae)//displays null if no obj is created in the java virtual machine.

But i think this is not the preferred way to get the solution
so, please anyone tell me
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try exception.printStackTrace(). You need to see the first line of the stack trace - that tells you exactly which line in which class is the problem. It's probably preferable to use logging instead - the servlet has a log() method which takes a String message and a Throwable. Better yet, if you have Log4J or another logging framework set up, use that. Always make sure you're using a log method that accepts a Throwable parameter, so that it's treated as an exception and logged properly, with the complete stack trace. Don't use exception.toString() or exception.getMessage() to convert to String, unless you're also passing the complete exception as another parameter. The toString() and getMessage() methods lose valuable information, namely the stack trace.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what if i use printStackTrace...
and one more doubt...
displaying the exception using System.out.pritnln(Exception);
is it similar to System.out.println(exception.PrintStackTrace());
if not can u tell me what is the difference with these to statements???

cinux
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just want to piont out that

the printStackTrace() method does not return anything....

you will get compilation errors if you use System.out.println(exception.printStackTrace());
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
which is the best way to trace out the nullpointerexception in java specially in jsp servlets/???
I use System.out.println(ClassNmae)//displays null if no obj is created in the java virtual machine.

But i think this is not the preferred way to get the solution
so, please anyone tell me


Some code tends to generate NullPointer Exceptions often.

I suggest the following strategies:

a. Do NOT put any java code in JSPs. At least, avoid it as far as possible.
b. In your servlets/beans, try using Validate.notNull() to ensure there are no null references.

The long-term solution to this is a better programming approach with greater cognizance of objects, their references and their values.
[ January 09, 2006: Message edited by: Stuart Ash ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx for ur valuable suggestion but Validate.notNull() is not a standard java class.
what should i do if i need to use that class in my jsp page..
hello boss, without java code we cannot develop a jsp page whenever we need to generate fields(data) dinamically.
I think there is no other way..

cinux
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!