我正在使用 TimerTask 运行 springboot 应用程序,我的服务对象显示为空。
我尝试了各种方法,但无法摆脱空指针异常。
主要类。
@SpringBootApplication
@ComponentScan(basePackages = {"com.comments.demo"})
public class NotifyMain {
@Autowired
static
NotifyService notifyService;
public static void main(String[] args) {
Timer timer1 = new Timer();
timer1.schedule(notifyService, 10, 10);
SpringApplication.run(NotifyMain.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
服务类
package com.comments.demo;
@Service
@Configurable
public class NotifyService extends TimerTask{
@Autowired
ListNotification listElement;
@Override
public void run() {
Notification notification= new Notification();
listElement.add(notification);
}
Run Code Online (Sandbox Code Playgroud)
ListNotification 和 Notification 类工作正常。
安慰
Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.sched(Timer.java:399)
at java.util.Timer.schedule(Timer.java:248)
at com.comments.demo.NotifyMain.main(NotifyMain.java:22)
Run Code Online (Sandbox Code Playgroud)
这是 ListNotification 的代码 …