faqts : Computers : Programming : Languages : Java : JSP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

119 of 229 people (52%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How do I debug a "Java Null Pointer Exception" in a JSP page?

Dec 24th, 2002 03:17
Danny, Carlos Orozco, Ravi Murthy,


Null pointer Exceptions are hard to find, they usually show when you 
try to access a property of an object and that object doesn't exist.

something like.

String name = (String)request.getAttribute("name");
int length = name.length;

As far as Null Pointer Exceptions are considered, follow the 
moral "PREVENTION IS BETTER THAN CURE". This means that all the 
possible lines of code that could generate this error have to be first 
listed out from the JSP. Once this is done, handle it like this...

String name = (String)request.getAttribute("name");
if (name==null)
  {
      System.out.println("name field null. cannot continue");
  }
int length = name.length;

This would throw up an message in the server window (I've seen this 
happening with weblogic 5.11 server)