NullPointerException on method (NativeMethodAccessorImpl.invoke0(Native Method) [duplicate]

I’m creating a scheduled method to create last month invoices (currently set to every 5 seconds for testing purposes). I’m getting a NullPointerException on the line where I’m trying to get shiftServices from my shiftServiceService.

@Scheduled(cron = "*/5 * * * * *")
    public void createLastMonthInvoices() {
        List<Client> clients = clientService.getClientList();
        System.out.println("Clients size: " + clients.size());
        for (Client client : clients) {
            System.out.println("Client ID: " + client.getId());
            List<ShiftService> shiftServices = shiftServiceService.getLastMonthShiftServicesByClient(client.getId());
            if (!ObjectUtils.isEmpty(shiftServices)) {
                System.out.println("Services size: " + shiftServices.size());
        }
    }
}

Here is the shiftServiceService method itself.

public List<ShiftService> getLastMonthShiftServicesByClient(Long clientId) {
        Specification<ShiftService> spec = where(null);
        spec = spec.and(client(clientId));
        spec = spec.and(lastMonthServices());
        spec = spec.and(scheduledServices());

        return shiftServiceRepository.findAll(spec);
    }

clientId is not null and outputs correctly, but the function itself is giving me an error like this:

java.lang.NullPointerException: null
    at com.bitweb.syda.service.invoice.InvoiceService.createLastMonthInvoices(InvoiceService.java:40)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

2 Answers

there can be one things you can check. One is shiftServiceService object is initialized or not.

You are printing a null variable

System.out.println("Client ID: " + client.getId());

Archive from: https://stackoverflow.com/questions/59064167/nullpointerexception-on-method-nativemethodaccessorimpl-invoke0native-method

Leave a Reply

Your email address will not be published. Required fields are marked *