0

我有一個動作註釋,它充當Jquery數據表的服務器端處理動作。同時獲取數據表的參數,我得到一個例外。爲獲得PARAMS類是:數據表參數爲空,導致NumberFormatException:空。任何方案?

package com.healthslate.reports.view.actions; 

import javax.servlet.http.HttpServletRequest; 

public class DataTablesParamUtility { 

public static JQueryDataTableParamModel getParam(HttpServletRequest request) 
{ 
    if(request.getParameter("sEcho")!=null || request.getParameter("sEcho")!= "") 
    { 
     JQueryDataTableParamModel param = new JQueryDataTableParamModel(); 
     param.sEcho = request.getParameter("sEcho"); 
     param.sSearch = request.getParameter("sSearch"); 
     param.sColumns = request.getParameter("sColumns"); 
     param.iDisplayStart = Integer.parseInt(request.getParameter("iDisplayStart")); 
     param.iDisplayLength= Integer.parseInt(request.getParameter("iDisplayLength")); 
     param.iColumns = Integer.parseInt(request.getParameter("iColumns")); 
     param.iSortingCols = Integer.parseInt(request.getParameter("iSortingCols")); 
     param.iSortColumnIndex = Integer.parseInt(request.getParameter("iSortCol_0")); 
     param.sSortDirection = request.getParameter("sSortDir_0"); 
     return param; 
    }else 
     return null; 
    } 
} 

我得到異常的 param.iDisplayStart = Integer.parseInt(request.getParameter("iDisplayStart"));

的例外是:

HTTP Status 500 - null 

type Exception report 

message null 

description The server encountered an internal error that prevented it from fulfilling this request. 

exception 

java.lang.NumberFormatException: null 
    java.lang.Integer.parseInt(Unknown Source) 
    java.lang.Integer.parseInt(Unknown Source) 
    com.healthslate.reports.view.actions.DataTablesParamUtility.getParam(DataTablesParamUtility.java:16) 
    com.healthslate.reports.view.actions.pageVisitReportModifiedAction.serverSideSearch(pageVisitReportModifiedAction.java:78) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    java.lang.reflect.Method.invoke(Unknown Source) 
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) 
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) 
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) 
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.intercept(SessionTransactionInjectorInterceptor.java:181) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    net.bull.javamelody.StrutsInterceptor.intercept(StrutsInterceptor.java:68) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) 
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563) 
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) 
    org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125) 
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs. 

回答

2

NumberFormatException當你試圖非法值轉換爲int值時發生。你需要你想讀他們爲int值前檢查值

//Check the `iDisplayStart` parameter is not null 
if(request.getParameter("iDisplayStart")!=null) 
//do the conversion if it is not null 
param.iDisplayStart = Integer.parseInt(request.getParameter("iDisplayStart")) 

注:iDisplayStart參數應包含一個有效的值,它可以被解析成一個int值。否則,您將獲得NumberFormatException。您還可以創建一個實用程序方法,它接受輸入值,驗證它並執行轉換,並返回解析的值int。如果在實用程序方法中發生任何異常,它將返回表示失敗或錯誤的值int

0

也有一些是被稱爲異常處理。你爲什麼先不這樣做? 把你的代碼放在一個try - catch塊中,找到確切的錯誤來源。

當您嘗試解析數字而不是字符串時,會發生數字格式異常。 首先檢查下面的代碼,看看你得到了什麼值,如果它的數字或字符串,請妥善做異常處理,否則即使你的代碼工作得很好,那將是你的第一次評論評論。 ATLEAST從我:)

request.getParameter("iDisplayStart") 
+2

iDisplayStart是數據表的字符串參數。 –