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
posted
0
You are doing fine with String. The problem must be with "item" object.
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
posted
0
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
posted
0
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
posted
0
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