2017-09-03 82 views
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>&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> 
    <% 
    } 
    } 
} 
%> 

當我使用的是本地的代碼然後我沒有得到任何錯誤時拋出,但只對託管服務器

堆棧跟蹤了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. 
+2

https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq=1 –

回答

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總是更好的。請讓我知道這可不可以幫你。

乾杯..!