我想将一些数据插入我的数据库。但我明白了
"ERROR: org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
java.lang.NullPointerException"
我不知道该怎么办。
public class HeadHunterImport {
@Autowired
private static HeadHunterService headHunterService;
@Scheduled(fixedRate = 600000)
public void AsyncRemovalOldData() {
headHunterService.addHeadHunter("Moscow", 100, 100) ;
}
如果我在 Controller 中调用它,它会正常工作。怎么了?
最佳答案
if(headHunterService!=null){
headHunterService.addHeadHunter("Moscow", 100, 100) ;
}else{
Sysem.out.println("headHunterService Object is null");
}
如果 headHunterService 返回 null,请确保您的上下文中存在以下代码。
<context:annotation-config/>
<context:component-scan base-package="your.package.name.here"/>
确保以下类使用@Component进行注释
@Component
class HeadHunterService {
}
or
you need the setter injection for headHunterService
关于java - 计划方法中的 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478647/