如何在jsp中为for循环处理NullPointerException?

时间:2017-09-03 10:35:01

标签: java jsp java-ee nullpointerexception

在这段代码中,我使用了Personal personal=new Personal();个对象。 但我想在这里处理空指针异常。请帮我解决这个问题。

<%
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>&nbsp;<%=sNo[i] %></td>
        <td>&nbsp;<%=hsnCode[i]%></td>
        <td>&nbsp;<%=modelNumber[i] %></td>
        <td>&nbsp;<%=serialNumber[i] %></td>
        <%-- <td>&nbsp;<%=labourFree[i] %></td> --%>
        <td>&nbsp;<%=rate[i] %></td>
        <td>&nbsp;<%=qty[i] %></td>
        <td>&nbsp;<%=gstSelect[i] %></td>
        <td>&nbsp;<%=amount[i] %></td>
    </tr>
    <%
     }
   }
}
%>
  

当我在本地使用此代码时,我没有得到任何激发但只在托管服务器上获得了

堆栈跟踪,

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.

1 个答案:

答案 0 :(得分:0)

在回答此问题之前,我想在您的代码中表达一些问题。根据第一条评论please refrain using java code inside jsp file

  

请访问该答案,以了解用户BalusC的简要说明。

现在回答您的问题,您只检查了对象personal不为空。但是在对象内部也许有可能获得空值。因此,在检查字符串是否为空之前不要拆分字符串。

String[] serialNumber, labourFree;
if(personal.getSerialNumber() != null){
   serialNumber[] = personal.getSerialNumber().split(",") ;
}
if(personal.getLabourFree() != null){
   labourFree[] = personal.getLabourFree().split(",") ;
}

对所有领域都这样做。在使用该值执行某些操作之前检查null总是更好。如果有帮助,请告诉我。

干杯..!