gpt4 book ai didi

java - 计划方法中的 NullPointer 异常

转载 作者:行者123 更新时间:2023-12-02 06:47:21 25 4
gpt4 key购买 nike

我想将一些数据插入我的数据库。但我明白了

"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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com