Calculating difference between two dates and writing it to the entity

Hey, I’m fairly new to Java and recently started using Cuba to create a planning application. One function i need for this is to calculate the difference between the date last executed and the current date.

I have looked around on internet and put together something which i think might be able to work, but its not quite working yet. Whenever i try to compile i get the following error:

Error:(3, 42) java: package com.company.machineplanning.entity does not exist.

My code looks like this:

package com.company.machineplanning.web.onderhoudsregel;

import com.company.machineplanning.entity.Onderhoudsregel;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.data.Datasource;
import java.time.ZoneId;
import java.time.LocalDate;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;

public class OnderhoudsregelBrowse extends AbstractLookup {

    @Inject
    public Datasource<Onderhoudsregel> datasource;

    @Inject
    public DataManager dataManager;

    public void onCalcDate (Component source) {
        Onderhoudsregel selectedOnderhoudsregel = datasource.getItem();
        if (selectedOnderhoudsregel == null) {
            showNotification("Selecteer een onderhoudsregel!");
            return;
        }

        Date input = selectedOnderhoudsregel.getLaatstUitgevoerd();
        LocalDate oud = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate huidig = LocalDate.now();

        long tussen = Math.abs(oud.getDayOfYear() - huidig.getDayOfYear());
        long tussenDagen = TimeUnit.DAYS.convert(tussen, TimeUnit.DAYS);

        int tussenDagenConverted = (int) tussenDagen;


        selectedOnderhoudsregel.setDagenGeleden(tussenDagenConverted);
        dataManager.commit(selectedOnderhoudsregel);
        datasource.refresh();

        showNotification("Klaar!", NotificationType.TRAY);
    }


}

If any of you can assist me, it would be amazing!

Kind regards,
Tim

Hi Tim,

Have you tried opening your code in an IDE?
If not, I would recommend installing IntelliJ IDEA and CUBA plugin for it as explained at https://doc.cuba-platform.com/manual-6.8/ide_integration.html. Then just press IDE button in Studio when the screen is selected and see what the IDE will tell you about your code.

Hey Konstantin,

I have installed IDEA and it is giving me the following error:

Error:(3, 42) java: package com.company.machineplanning.entity does not exist.

I have been looking around for it’s meaning but could unfortunately not find any helpful things.
The only entity i am using is

com.company.machineplanning.entity.Onderhoudsregel;

Then please provide the whole output from Studio messages panel after you click Build > Assemble project.

Let me elaborate a bit more on the situation for your understanding. In my browse screen I have a button which starts the code showed above, but whenever I press this button in the live enviroment I get the following error (Live enviroment):

java.lang.NullPointerException
at com.company.machineplanning.web.onderhoudsregel.OnderhoudsregelBrowse.onCalcDate(OnderhoudsregelBrowse.java:25)
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:498)
at com.haulmont.cuba.gui.xml.DeclarativeAction.actionPerform(DeclarativeAction.java:84)
at com.haulmont.cuba.web.gui.components.WebButton.performAction(WebButton.java:44)
at com.haulmont.cuba.web.gui.components.WebButton.lambda$new$61446b05$1(WebButton.java:36)
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:498)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:200)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:163)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1037)
at com.vaadin.ui.Button.fireClick(Button.java:377)
at com.haulmont.cuba.web.toolkit.ui.CubaButton.fireClick(CubaButton.java:54)
at com.vaadin.ui.Button$1.click(Button.java:54)
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:498)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:119)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:444)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:409)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1435)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:361)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:300)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:191)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:107)
at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:73)
at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

The studio message looks as follows when building:

[13:15:48.262] Ready
[13:16:03.484] Building the project
[13:16:03.485] Executing Gradle task: assemble
:app-core:assembleDbScripts UP-TO-DATE
:app-core:dbScriptsArchive UP-TO-DATE
:app-core:beansXml UP-TO-DATE
:app-global:beansXml UP-TO-DATE
:app-global:processResources UP-TO-DATE
:app-global:buildInfo UP-TO-DATE
:app-global:compileJava UP-TO-DATE
:app-global:enhance UP-TO-DATE
:app-global:classes UP-TO-DATE
:app-global:jar UP-TO-DATE
:app-core:compileJava NO-SOURCE
:app-core:processResources UP-TO-DATE
:app-core:classes UP-TO-DATE
:app-core:jar UP-TO-DATE
:app-core:sourceJar UP-TO-DATE
:app-core:assemble UP-TO-DATE
:app-global:sourceJar UP-TO-DATE
:app-global:assemble UP-TO-DATE
:app-gui:beansXml UP-TO-DATE
:app-gui:compileJava NO-SOURCE
:app-gui:processResources UP-TO-DATE
:app-gui:classes UP-TO-DATE
:app-gui:jar UP-TO-DATE
:app-gui:sourceJar UP-TO-DATE
:app-gui:assemble UP-TO-DATE
:app-web:beansXml UP-TO-DATE
:app-web:compileJava UP-TO-DATE
:app-web:processResources UP-TO-DATE
:app-web:classes UP-TO-DATE
:app-web:jar UP-TO-DATE
:app-web:sourceJar UP-TO-DATE
:app-web:webArchive UP-TO-DATE
:app-web:assemble UP-TO-DATE

BUILD SUCCESSFUL in 0s
23 actionable tasks: 23 up-to-date

Again, I am sorry if I’m missing something really obvious, I’m new to Cuba and Java and I really appreciate the help!

So the compilation works fine. This is very different from what you asked initially.

java.lang.NullPointerException
at com.company.machineplanning.web.onderhoudsregel.OnderhoudsregelBrowse.onCalcDate(OnderhoudsregelBrowse.java:25)

What is on line 25 of class OnderhoudsregelBrowse? Look at what variable can be null on this line.

Sorry for mixing up the terms! Line 25 has the following code on it:

Onderhoudsregel selectedOnderhoudsregel = datasource.getItem();

datasource refers to the code written on line 18 and 19 which is:

@Inject
public Datasource<Onderhoudsregel> datasource;

Could it possibly be that this code on line 18 and 19 gives no value? What I am trying to archieve is to edit the rows which the user has selected. When the button is pressed and the CalcDate action is called, the selected rows should be updated.

I got to this code by using the Live demo tutorial about business controllers
(https://github.com/cuba-platform/sample-business-logic/blob/master/modules/web/src/com/company/sample/web/ex1/customer/CustomerBrowse.java)

No, in this case it means datasource is null. Therefore datasource. getItem() throws a NPE.
Can you share the datasource definition in the corresponding XML file? I think you just named the variable not as defined in the XML.

You were right! I changed this datasource (for whatever reason) without thinking of it. In my XML file it was defined as onderhoudsregelDS (like in the example) but in my code i just named it datasource. It works perfect now.

Thanks a lot both of you!