2013-05-01 177 views
0

這是我的代碼,其執行兩種方法:java.lang.NumberFormatException:對於輸入字符串:「類型」

<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) 
+1

是的,完整的堆棧跟蹤會有所幫助。 – 2013-05-01 11:59:45

回答

-1

你肯定沒有拖尾空格或領先的空格

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

這不會成爲問題 - 從例外情況可以清楚地看出,系統試圖將字符串''type''解析爲數字(並且意料之中的是,失敗)。 – 2013-05-01 12:06:24

-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) 

因此,您應該檢查佈局文件其他位置的表格中的某些鏈接。

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,這樣的事情在原始的Java代碼來訪問數組中的EL:

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

這顯然是錯誤的。堆棧跟蹤提示,你已經一個

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

本身暗示customerPaymentsList<Object[]>,而不是在違背一個List<CustomerPayment>到您的預期和(錯誤!)downcasted的異常。您不會選擇c,如SELECT c FROM CustomerPayment c,但僅限於單個屬性,因此每條記錄都表示爲Object[]

其中一個解決方案是堅持List<Object>並使用索引而不是屬性名來訪問它們。您選擇c.type爲第1列,所以它有數組索引0

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