pmaingi
pmaingi

Reputation: 121

Getting total amount in a column

I have the below method

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

}

and the type is below

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

and in the model i have the below

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

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

When i try to sum up the totals i only get the first column as the total. See below logs

2222 : 460.0
not a number

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

See below stack trace

INFO: Installed AtmosphereInterceptor Track Message Size Interceptor using | with priority 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

Upvotes: 2

Views: 632

Answers (1)

Akash Thakare
Akash Thakare

Reputation: 22972

First of all you are setting column footer inside loop your following code should be outside the loop,

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

Secondly you are casting to String and using toString as well use one of them,during your second iteration value you are getting can not be parsed to double so that you got not a number which you are printing int catch block so to get more info about exact problem for that you should use e.printStackTrace(); in catch block.

2222 : 460.0
not a number

SO ,from stacktrace we can see that the value you are getting from Amount property is

2,336.00
 ^

So this value can not be parsed to double due to , in String the easy solution can be the use of #replace and you better use null check before parsing so you can avoid null pointer excaption.

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

Upvotes: 4

Related Questions