我想将一些数据插入数据库。但是我抓住了

"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) ;

    }


如果我在控制器中调用它,它将正常工作。怎么了?

最佳答案

 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

10-01 00:53