Reputation: 13844
I have the following code:
String table;
private DB.ConnectMAS mas=new DB.ConnectMAS();
java.sql.ResultSet rs1 = mas.DBquery("select * from products");
try{
String duedate = "";
while(rs1.next()){
String duedate = rs1.getString("duedate");
table+="<tr><td>"+duedate.substring(1,10)+"</td></tr>";
} catch(java.sql.SQLException e){}
It is throwing an error on duedate.substring(1,10)
I'm trying to understand why this is incorrect? thanks in advance
error:
SEVERE: Servlet.service() for servlet POList threw exception
java.lang.NullPointerException
at POList.getDetails(POList.java:126)
at POList.getTable(POList.java:165)
at POList.processRequest(POList.java:55)
at POList.doGet(POList.java:177)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:834)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
at java.lang.Thread.run(Unknown Source)
Upvotes: 0
Views: 2313
Reputation: 27464
The first String duedate is superfluous. The variable is redeclared inside the loop.
But the part causing the exception is likely that you don't check for duedate coming back as null. Try something like:
try{
while(rs1.next()){
String duedate = rs1.getString("duedate");
if (duedate==null && duedate.length()>=10)
table+="<tr><td>Date missing or invalid</td></tr>"; // or whatever text is appropriate
else
table+="<tr><td>"+duedate.substring(1,10)+"</td></tr>";
} catch(java.sql.SQLException e){}
I also wonder why you extracting positions 1 - 9 of duedate. Do you realize that substring considers the first position to be zero, and that the second parameter is the first character to NOT include? If duedate is, say, "2010-07-05", this will give "010-07-05". If you're trying to get the first 10 characters, you want substring(0,10). Of course I don't know what's really in your duedate field.
Upvotes: 0
Reputation: 17149
You are declaring String duedate
inside your While Loop. So once rs1.next()
returns false, it breaks from the While Loop and duedate
goes away (out of scope of While Loop). No big deal since you declare it again outside the scope of the While Loop. However, by then, rs1
contains no more entries so rs1.getString("duedate")
returns null.
You need to either declare String duedate
outside before the scope of the While Loop OR, like HankGay mentioned, move the table+=
into the While Loop.
Upvotes: 1
Reputation: 116246
First of all, I suppose rs
should be rs1
on line 10 of your code sample. Even so, I guess that by that time rs1
has no more lines left, so getString
returns null
, which results in a NullPointerException on the next line.
Even if that were not the case, the string returned may not be 10 characters long, in which case substring(1,10)
fails with an ArrayIndexOutOfBoundsException.
Upvotes: 3
Reputation: 70909
I see the potential for two possible issues, the first is a NullPointerException in
duedate.substring(1,10)
because you don't really know if
String duedate = rs1.getString("duedate");
was set to a value or null. Perhaps there is no returned String in the rs1.getString(...) call.
The other potential issue is that you never check that duedate is more than 10 characters long. If you ask for a substring, then it should be within the returned String. For example, I would expect the code:
String abc = "abc";
abc.substring(4, 24);
to throw an exception, probably an ArrayOutOfBoundsException, but as long as it threw some exception, it would be proper.
Upvotes: 0
Reputation: 114757
In case of a NullPointerException
- duedate
is null
, which is equal to a NULL value in the record
In case of a IndexOutOfBoundsException
- duedate
is less then 10 characters long, so the value in the database is either incorrect (if you expect entries with more than 10 chars) or even empty.
Anyway - expect, that duedate
may be null
, empty or too short and just add some code behind String duedate = rs.getString("duedate");
to handle those cases.
Upvotes: 0
Reputation: 71939
Like @danben says, you need to post the error to get meaningful help. Looking at your code, though, I'd imagine that duedate
at the time of the error is null
. You probably need to move the code that's actually using duedate
inside your while
loop.
Upvotes: 4