获取列中的总金额

时间:2014-10-23 11:50:08

标签: java vaadin

我有以下方法

 public void footerSet() {

    // Calculate total sum
    double totalSum = 0.0;
    // double totalMargin = 0.0;
    for (int i = 0; i < ic.size(); i++) {
        Item item = ic.getItem(ic.getIdByIndex(i));

        try {

            double valuetotal = Double.parseDouble((String) item
                    .getItemProperty("Amount").getValue());

            totalSum += valuetotal;
            System.out.println(">>>2222 : " + totalSum);

            // System.out.println(">>>2222 : " + totalSum);
        } catch (NumberFormatException e) {
            // System.out.println("not a number");
            e.printStackTrace();
        }
    }
    ProductTableGlobal.setColumnFooter("Outlet", "Total");
    ProductTableGlobal.setColumnFooter("Amount", String.valueOf(totalSum));

}

,类型在

之下
    icLoaded.addContainerProperty("Outlet", String.class, null);
    icLoaded.addContainerProperty("Amount", String.class, null);

在模型中我有以下

     //NumberFormat numberFormat = new DecimalFormat("#,###.00");

      double totalAmounts = totalAmount;
     passTable.getContainerProperty(UID, "Amount").setValue(
                numberFormat.format((totalAmounts)));

当我试图总结总数时,我只得到第一列作为总数。见以下日志

  
    
      

2222:460.0
           不是数字

    
  
>>>2222 : 460.0
>>>2222 : 460.0

见下面的堆栈跟踪

INFO:安装的AtmosphereInterceptor跟踪消息大小拦截器使用|优先级为BEFORE_DEFAULT

  
    
      

2222:460.0

    
  
java.lang.NumberFormatException: For input string: "2,336.00"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
at java.lang.Double.parseDouble(Double.java:540)
at com.zocha.reports.TaxByOutlet.footerSet(TaxByOutlet.java:280)
at com.zocha.reports.TaxByOutlet.OutletSalesView(TaxByOutlet.java:84)
at com.zocha.reports.TaxByOutlet.enter(TaxByOutlet.java:62)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:571)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:526)
at valo.ValoThemeUI$5.buttonClick(ValoThemeUI.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:978)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:287)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:180)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:93)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

>>>2222 : 460.0
>>>2222 : 460.0

Note: 2,336.00 is the second value that needs to be added and so on

1 个答案:

答案 0 :(得分:4)

首先,您要在循环内设置列页脚,以下代码应该在循环外部,

ProductTableGlobal.setColumnFooter("Outlet", "Total");
ProductTableGlobal.setColumnFooter("Amount",String.valueOf(totalSum));

其次你要转向String并使用toString并使用其中一个,在你获得的第二个迭代值中,无法解析加倍,以便你有不是数字,你打印int catch块,以获得更多关于确切问题的信息,你应该在catch块中使用e.printStackTrace();

2222 : 460.0
not a number

所以,从stacktrace我们可以看到你从Amount属性得到的值是

2,336.00
 ^

因此,中的String导致此值无法解析为简单解决方案可以使用#replace并且您最好在解析之前使用null检查,这样您就可以避免空指针excaption。

double valuetotal=0.0;
if(item!=null && item.getValue()!=null){
  String tempString = item.getItemProperty("Amount")
                          .getValue()
                          .toString()
                          .replace(",","");
  valuetotal=Double.parseDouble(tempString);
}