Passing CollectionDatasource parameter to Service and get calculated value

Hi
I have implement calculation of Sales order amount from Controller and now I want this to do it from Service. I suppose I have to send the CollectionDatasource of OrderLine Entity from controller to Service to ServiceBean. Can you please share the codes?

Here is the SalesOrderServiceBean


@Service(SalesService.NAME)
public class SalesServiceBean implements SalesService {

    @Inject
    private CollectionDatasource<SalesOrderLine, UUID> salesOrderLineDs;

    private BigDecimal calculateSalesOrderAmount(CollectionDatasource<SalesOrderLine, UUID> salesOrderLineDs){
        BigDecimal amount= BigDecimal.ZERO;
        for (SalesOrderLine line: salesOrderLineDs.getItems()){

            amount = amount.add(line.getQuantity().multiply(line.getPrice()));
        }
        return amount;

    }

}

Service


public interface SalesService {
    String NAME = "inteaccgms_SalesService";


    void calculateSalesOrderAmount(Collection salesOrderLineDs);

}

Controller


public class SalesOrderEdit extends AbstractEditor<SalesOrder> {
    @Inject
    private CollectionDatasource<SalesOrderLine, UUID> salesOrderLineDs;

    @Inject
    SalesService service;

    @Override
    public void init(Map<String, Object> params) {

        salesOrderLineDs.addCollectionChangeListener(e-> service.calculateSalesOrderAmount(salesOrderLineDs));
    }

}

I think it will be better to pass a collection of salesOrderLines as an argument to the service. CollectionDatasource class is unavailable in the core module where your service implementation is located by default.
So your service method definition will look like this:

public interface SalesService {
    ...
    BigDecimal calculateAmount(Collection<SalesOrderLine> lines);
}

and the listener in screen controller will be like this:

salesOrderLineDs.addCollectionChangeListener(e -> salesService.calculateAmount(getItem().geSalesOrderLines()));

Hi
Thank you. I have modified the program but currently having the following null pointer exception:


java.lang.NullPointerException
    at com.inteacc.gms.gui.salesorder.SalesOrderEdit.init(SalesOrderEdit.java:32)
    at com.haulmont.cuba.gui.WindowManager.init(WindowManager.java:731)
    at com.haulmont.cuba.gui.WindowManager.initWrapperFrame(WindowManager.java:722)
    at com.haulmont.cuba.gui.WindowManager.createWindow(WindowManager.java:176)
    at com.haulmont.cuba.gui.WindowManager.openEditor(WindowManager.java:454)
    at com.haulmont.cuba.gui.components.WindowDelegate.openEditor(WindowDelegate.java:220)
    at com.haulmont.cuba.web.gui.WebWindow.openEditor(WebWindow.java:399)
    at com.haulmont.cuba.gui.components.actions.CreateAction.internalOpenEditor(CreateAction.java:209)
    at com.haulmont.cuba.gui.components.actions.CreateAction.actionPerform(CreateAction.java:205)
    at com.haulmont.cuba.web.gui.components.WebButton.performAction(WebButton.java:43)
    at com.haulmont.cuba.web.gui.components.WebButton$1.buttonClick(WebButton.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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:1003)
    at com.vaadin.ui.Button.fireClick(Button.java:393)
    at com.haulmont.cuba.web.toolkit.ui.CubaButton.fireClick(CubaButton.java:42)
    at com.vaadin.ui.Button$1.click(Button.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:313)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:202)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:95)
    at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.lambda$synchronizedHandleRequest$67(CubaVaadinServletService.java:290)
    at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler$$Lambda$17/1237553476.call(Unknown Source)
    at com.haulmont.cuba.web.sys.CubaVaadinServletService.withUserSession(CubaVaadinServletService.java:171)
    at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.synchronizedHandleRequest(CubaVaadinServletService.java:290)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:356)
    at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:226)
    at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:149)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    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 com.haulmont.cuba.web.sys.CubaHttpFilter.handleNotFiltered(CubaHttpFilter.java:100)
    at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:87)
    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:502)
    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:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

My program is:

edit controller


ublic class SalesOrderEdit extends AbstractEditor<SalesOrder> {

    @Inject
    private CollectionDatasource<SalesOrderLine, UUID> salesOrderLineDs;

    @Inject
    SalesOrderService salesService;

    @Override
    public void init(Map<String, Object> params) {
        
        salesOrderLineDs.addCollectionChangeListener(e -> salesService.calculateSalesOrderAmount(getItem().getLines()));

    }

}

Service


public interface SalesOrderService {
    String NAME = "inteaccgms_SalesOrderService";

    BigDecimal calculateSalesOrderAmount(Collection<SalesOrderLine> lines);

}

Bean


@Service(SalesOrderService.NAME)
public class SalesOrderServiceBean implements SalesOrderService {

   // @Inject
  //  private CollectionDatasource<SalesOrderLine, UUID> salesOrderLineDs;
    public BigDecimal calculateSalesOrderAmount(Collection<SalesOrderLine> orderLines){
        BigDecimal amount= BigDecimal.ZERO;
        for (SalesOrderLine line: orderLines){
            amount = amount.add(line.getQuantity().multiply(line.getPrice()));
        }
        return amount;
    }

}

Thanks for your further help.

Put a breakpoint at the line you get a NPE. Which object is null?

Not sure how to identify that, any further help will be appreciated

As we can see


java.lang.NullPointerException 
    at com.inteacc.gms.gui.salesorder.SalesOrderEdit.init(SalesOrderEdit.java:32) 

the problem happened in the


    public void init(Map<String, Object> params) { 
        salesOrderLineDs.addCollectionChangeListener(e -> salesService.calculateSalesOrderAmount(getItem().getLines())); 
    } 

method, you developed for your application.

Unfortunately, we cannot provide you with the answer until you debug your sourcecode and find the NULL object.

If you mean that you have no idea how to debug the sourcecode then read the following:
https://doc.cuba-platform.com/manual-6.0/debug_setup.html (note that Studio runs tomcat with a debug.bat or debug.sh by default)
https://www.jetbrains.com/help/idea/15.0/creating-and-editing-run-debug-configurations.html
https://www.jetbrains.com/help/idea/15.0/debugging.html