0
在此代碼中,我使用了Personal personal=new Personal();
對象。 但我想在這裏處理空指針異常。請幫我解決這個問題。如何在jsp中處理for循環中的NullPointerException?
<%
if(personal!=null) {
String sNo[] = personal.getsNo().split(",") ;
String hsnCode[]=personal.getHsnCode().split(",");
String modelNumber[] = personal.getModelNumber().split(",") ;
String serialNumber[] = personal.getSerialNumber().split(",") ;
String labourFree[] = personal.getLabourFree().split(",") ;
String rate[] = personal.getRate().split(",") ;
String qty[] = personal.getQuantity().split(",") ;
String gstSelect[] = personal.getGst().split(",") ;
String amount[] = personal.getAmount().split(",") ;
if(modelNumber.length>0){
for(int i=0;i<modelNumber.length;i++){
%>
<tr>
<td> <%=sNo[i] %></td>
<td> <%=hsnCode[i]%></td>
<td> <%=modelNumber[i] %></td>
<td> <%=serialNumber[i] %></td>
<%-- <td> <%=labourFree[i] %></td> --%>
<td> <%=rate[i] %></td>
<td> <%=qty[i] %></td>
<td> <%=gstSelect[i] %></td>
<td> <%=amount[i] %></td>
</tr>
<%
}
}
}
%>
當我使用的是本地的代碼然後我沒有得到任何錯誤時拋出,但只對託管服務器
堆棧跟蹤了excption,
org.apache.jasper.JasperException: An exception occurred processing JSP page /completeDetail.jsp at line 124
121: <%
122:
123: if(personal!=null) {
124: String sNo[] = personal.getsNo().split(",") ;
125: String hsnCode[]=personal.getHsnCode().split(",");
126: String modelNumber[] = personal.getModelNumber().split(",") ;
127: String serialNumber[] = personal.getSerialNumber().split(",") ;
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
org.apache.jsp.completeDetail_jsp._jspService(completeDetail_jsp.java:211)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.59 logs.
https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq=1 –