• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unknown cause of NullPointerException in JSP

 
colby brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just trying to base a conditional statement on the result of a method call that returns a String value. I seem to get a NullPointerException while checking for the exception. Anyone have any ideas? Here is the section of code:
<% String s = item.getOrderNumber(); %>
<% if(s != null) { %>
Some HTML here
<% } else { %>
Some other HTML here
<% } %>
I'm not exactly sure where the NullPointerException is occuring in this, but when I remove this things get generated fine.

------------------
Cb
 
Mahesh Rana
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing fine with String.
The problem must be with "item" object.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that 'item' is non-null?
 
colby brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For verification on the "item" not being null, the following code works fine.
<TD><A HREF="javascript:submitDoc('DisplayDocs','ts=<%=Globals.URLEncode(item.getTimeStamp())%>&order=<%=item.getOrderNumber()%>&doctype=ack');">Acknowledgement</A>&l t;/TD>
The generated result is:
<TD><A HREF="javascript:submitDoc('DisplayDocs','ts=2001-09-19-09.02.54.444173&order=null&doctype=ack');">Acknowledgement</A></TD>
I replace with this to catch the null.
<% String s = item.getOrderNumber(); %>
<TR>
<% if(s != null) { %>
<TD><A HREF="javascript:submitDoc('DisplayDocs','ts=<%=Globals.URLEncode(item.getTimeStamp())%>&order=<%=item.getOrderNumber()%>&doctype=ack');">Acknowledgement</A>&l t;/TD>
<% } else { %>
<TD>Acknowledgement</TD>
<% } %>
</TR>
</TABLE>
<% } %>
The goal is to catch the case where a null is produced and not show the link. But when I check for the null, it throws the NullPointerException. I have no idea why.

------------------
Cb
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, surely there is a stack trace for that exception (if not, you can surround your page with a try... catch and display it yourself). Every application server I know can be persuaded to stick the translated java source for your page in a directory somewhere. With these two ingredients you will know exactly where the NullPointerException is being thrown.
- Peter
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be the method getOrderNumber() contains code which throws the exception. u shud place log statements before and after the call to item.getOrderNumber() and also within the method to verify this.
or u can have the statement "item.getOrderNumber()" in a try/catch block in the jsp. (just to check if it is indeed the call to "item.getOrderNumber()" that is throwing the excetpion)
 
Jayanthimeena
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai all
First enclose it in try catch block and catch the exception and print in for reference.
Inside this method getOrderNumber(); use System.out.println() and check out where exactly it is happening.
Third
in this line
<% String s = item.getOrderNumber(); %>
check this
<%
try
{
String s = item.getOrderNumber()==null?"":item.getOrderNumber();
if(s.equals(""))
{
Some HTML here
}
else
{
Some other HTML here
}
}
catch (Exception e)
{
System.out.println("error "+e);
}
%>
try above thing by just copying it.
Send result.
Bye
Jayanthi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic