Java 方法上的NullPointerException(NativeMethodAccessorImpl.invoke0(本机方法)
我正在创建一个计划的方法来创建上个月的发票(为了测试目的,当前设置为每5秒一次)。我在试图从我的shiftServiceService获取shiftServices的行上得到一个NullPointerExceptionJava 方法上的NullPointerException(NativeMethodAccessorImpl.invoke0(本机方法),java,spring,spring-boot,Java,Spring,Spring Boot,我正在创建一个计划的方法来创建上个月的发票(为了测试目的,当前设置为每5秒一次)。我在试图从我的shiftServiceService获取shiftServices的行上得到一个NullPointerException @Scheduled(cron = "*/5 * * * * *") public void createLastMonthInvoices() { List<Client> clients = clientService.getClientL
@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());
}
}
}
您正在打印一个空变量
System.out.println("Client ID: " + client.getId());
有一件事你可以检查。一件是shiftServiceService对象是否初始化。看起来对象shiftServiceService没有初始化,仍然为空。你需要显示
InvoiceService
如何声明shiftServiceService
属性。@RahulAgrawal哦,我的天啊,你说得对……麻烦你了。哑巴error.check InvoiceService.java行号40正如我所说,clientId打印正确,例如:Client ID:35
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)
System.out.println("Client ID: " + client.getId());