Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Unknown cause of NullPointerException in JSP

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are doing fine with String.
The problem must be with "item" object.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that 'item' is non-null?
 
colby brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Grow your own food... or this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic