java.lang.NumberFormatException:对于输入字符串:“type”

时间:2013-05-01 11:37:58

标签: java hibernate jsf jpa

这是我执行两种方法的代码:

<h:panelGroup> <br /><br />  
    <h:outputLabel id="yearLbl" value="Year:" />
    <h:outputLabel id="selectType" /><br />
    <h:selectOneMenu id="type" value="#{clController.type}">
        <f:selectItem itemValue="2013" itemLabel="2013" />
        <f:selectItem itemValue="2012" itemLabel="2012" />
        <f:selectItem itemValue="2011" itemLabel="2011" />
        <f:selectItem itemValue="2010" itemLabel="2010" />
        <f:selectItem itemValue="2009" itemLabel="2009" />
        <f:selectItem itemValue="2008" itemLabel="2008" />
    </h:selectOneMenu>
    <h:commandButton type="submit" onclick="#{clController.getTotal(clController.type)}" action="#{clController.getPaymentByMonth(clController.type)}"  id="stateInfo"  value="Show Monthly "  >              
    </h:commandButton>
</h:panelGroup>

这些是名为:

的函数
public DataModel getPaymentByMonth(String year) {            
    this.session = HibernateUtil.getSessionFactory().getCurrentSession();

    List<CustomerPayment> paymentList = null;
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from CustomerPayment where DATE like '%" + year + "'");
        paymentList = (List<CustomerPayment>) q.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return paymentDataModel = new ListDataModel(paymentList);    
}

public DataModel getTotal(String year) {
    this.session = HibernateUtil.getSessionFactory().getCurrentSession();

    List<CustomerPayment> total = null;
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        System.out.println(year);
        Query q = session.createQuery("select c.type, c.date, sum(c.amount) from CustomerPayment c  where c.date  LIKE '%"+year+"' group by c.type");
        System.out.println("select c.type, c.date, sum(c.amount) from CustomerPayment c  where c.date  LIKE '%"+year+"' group by c.type");
        total = (List<CustomerPayment>) q.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return totalDataTable = new ListDataModel(total);    
}

问题是我收到了这个错误:

java.lang.NumberFormatException: For input string: "type"

该类型意味着多年,但我给出了错误。当我打印类型到屏幕时,它打印为2013年为什么我会收到此错误?

完整堆栈跟踪:

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
    at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:44)
    at org.apache.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:50)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.OutputLinkRenderer.encodeChildren(OutputLinkRenderer.java:124)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.TableRenderer.renderRow(TableRenderer.java:385)
    at com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:162)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:424)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:679)

3 个答案:

答案 0 :(得分:1)

java.lang.NumberFormatException: For input string: "type"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
    at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)

异常是告诉您正在尝试使用非整数索引type访问EL中的数组,在原始Java代码中是这样的:

Object[] array = getItSomehow();
array["type"];

这显然是错误的。堆栈跟踪暗示您已经

<h:dataTable value="#{bean.customerPayments}" var="customerPayment">
    ...
    <h:inputText value="#{customerPayment.type}" />

异常本身暗示customerPaymentsList<Object[]>而不是List<CustomerPayment>,与您的预期和(错误!!)下降相反。您不是在c中选择SELECT c FROM CustomerPayment c,而是仅选择单个属性,因此每条记录都表示为Object[]

其中一个解决方案是坚持List<Object>并使用索引而不是属性来访问它们。您选择c.type作为第一列,因此它具有数组索引0

<h:dataTable value="#{bean.customerPayments}" var="customerPayment">
    ...
    <h:inputText value="#{customerPayment[0]}" />

答案 1 :(得分:-1)

你确定没有拖尾的白色空间或前方空白

new Integer("2013") - Woks
new Integer(" 2013") - will not work

答案 2 :(得分:-1)

好吧,让我们分析一下。根据完整的堆栈跟踪。

此错误来自渲染阶段。

at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)

所以它发生在动作方法调用之后。

当OutputLink中的某个子组件尝试编码值(渲染值)

时抛出此错误
at com.sun.faces.renderkit.html_basic.OutputLinkRenderer.encodeChildren(OutputLinkRenderer.java:124)

并且OutputLink似乎生活在某些表格中。

at com.sun.faces.renderkit.html_basic.TableRenderer.renderRow(TableRenderer.java:385)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:162)

因此,您应该检查布局文件其他位置的表格中的某些链接。