JSP中的数字字段没有值时出现空指针异常

时间:2011-07-10 14:58:37

标签: jsp bean-validation

我正在使用bean来验证JSP的形式。如果使用setProperty并且表单上的某些字段留空,则会抛出空指针异常。仅当字段恰好是bean中的数字时才会发生此异常。我不能在不调用bean的setProperty的情况下进行验证。有没有其他方法可以做到这一点?我使用的代码如下。

<jsp:useBean id="addBean" scope="request" class="org.mypackage.products.add_product6_Variables" />
    <jsp:setProperty name="addBean" property="*" />

我的错误:

org.apache.jasper.JasperException: An exception occurred processing JSP page /response.jsp at line 19

19: jsp:setProperty name="addBean" property="*" />

type Exception report

message 

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

exception 

org.apache.jasper.JasperException: org.apache.jasper.JasperException: org.apache.jasper.JasperException: error in invoking method
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

org.apache.jasper.JasperException: org.apache.jasper.JasperException: error in invoking method
    org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:361)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspect(JspRuntimeLibrary.java:286)
    org.apache.jsp.response_jsp._jspService(response_jsp.java:77)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

org.apache.jasper.JasperException: error in invoking method
    org.apache.jasper.runtime.JspRuntimeLibrary.createTypedArray(JspRuntimeLibrary.java:533)
    org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:350)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspect(JspRuntimeLibrary.java:286)
    org.apache.jsp.response_jsp._jspService(response_jsp.java:77)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

java.lang.NumberFormatException: empty String
    sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
    java.lang.Float.valueOf(Float.java:388)
    org.apache.jasper.runtime.JspRuntimeLibrary.createTypedArray(JspRuntimeLibrary.java:516)
    org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:350)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspect(JspRuntimeLibrary.java:286)
    org.apache.jsp.response_jsp._jspService(response_jsp.java:77)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


1 个答案:

答案 0 :(得分:2)

您的bean属性类型是原始数字还是包装对象类型?我是指int还是Integer?

它现在可能是int类型,因为int和其他原语不能设置为null;而Integer对象可以设置为null。

您可以将bean属性类型更改为Integer等,并检查NPE是否仍然存在?