预定方法中的NullPointer异常

时间:2013-08-28 02:54:36

标签: java spring hibernate nullpointerexception

我想在我的数据库中插入一些数据。但我抓住了

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

    }

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

1 个答案:

答案 0 :(得分:2)

 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