NullPointer being thrown despite check
I have a select list in an HTML file the eventually has the selected
item passed to a servlet. However, if I don't select an item in the
list, it seems to throw a NullPointerException. I was hoping to catch
this with an if/else statement (as seen below), but it doesn't seem to
be working. Other than catching the exception are there other ways to
deal with this problem? To be sure, is this what is causing the exception?
Thanks! Code snippet below:
PW.println("<p>Please fill in the following: </p>");
PW.println("<form ACTION=\"ViewProductInfoSV\"
METHOD=\"POST\">");
PW.println("<p>Category List:</p>");
PW.println("<p><select NAME=\"CategoryList\" MULTIPLE
size=\"5\">");
while (myRSet.next()) {
PW.println("<option
value=\""+myRSet.getString("category_id")+"\">"+myRSet.getString("description"));
}
PW.println("</select></p>");
PW.println("<p>Code: <input TYPE=\"TEXT\" NAME=\"catCode\"
size=\"20\"></p>");
PW.println("<p>Description: <input TYPE=\"TEXT\"
NAME=\"catDesc\" size=\"20\"></p>");
PW.println("<p><input TYPE=\"SUBMIT\"> </p>");
PW.println("</form>");
PW.println("<br /><br />");
PW.println("The following information was selected:<br />");
//The problem appears to be here
for (int i=0; i < theCatList.length; i++) {
if (!theCatList[i].equals("")) {
PW.println("From the Category List: " +
theCatList[i]+"<br />");
}else{
PW.println("A selection must be made from the
category list");
}
}
PW.println("Code entered: "+ theCatCode+"<br />");
PW.println("Description Entered: "+ theCatDesc+"<br />");
theC.close();
}
catch (ClassNotFoundException ce) { PW.println("DB Error: " +
ce); }
catch (SQLException se) { PW.println("SQL Error: " + se); }
PW.println("</body></html>");
PW.close();
}
}
//Stack Trace Follows:
Servlet.service() for servlet viewProdInf threw exception
java.lang.NullPointerException
at ViewProductInfo.doGet(ViewProductInfo.java:59)
at ViewProductInfo.doPost(ViewProductInfo.java:12)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731)
at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)