File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Unknown cause of NullPointerException in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unknown cause of NullPointerException in JSP" Watch "Unknown cause of NullPointerException in JSP" New topic
Author

Unknown cause of NullPointerException in JSP

colby brown
Greenhorn

Joined: Sep 19, 2001
Posts: 4
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


Cb
Mahesh Rana
Ranch Hand

Joined: Sep 05, 2001
Posts: 139
You are doing fine with String.
The problem must be with "item" object.


SCJP2
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Are you sure that 'item' is non-null?
colby brown
Greenhorn

Joined: Sep 19, 2001
Posts: 4
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jun 07, 2000
Posts: 87
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

Joined: Jul 24, 2001
Posts: 17
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unknown cause of NullPointerException in JSP
 
Similar Threads
Need help with regular expression
Struts: FormBean does not see String[] from JSP
Problem with DbConnectionBroker
JSP
Can someone help me with a regular expression?