quartz和spring结合,报空指针错误:java.lang.NullPointerException

一  使用spring管理quartz,在启动服务器的时候,报错:

13:39:09,448 ERROR JobRunShell:211 - Job DEFAULT.promotionJob threw an unhandled Exception: 
java.lang.NullPointerException
	at cn.itcast.bos.quartz.PromotionJob.execute(PromotionJob.java:23)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
13:39:09,453 ERROR ErrorLogger:2425 - Job (DEFAULT.promotionJob threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
	at cn.itcast.bos.quartz.PromotionJob.execute(PromotionJob.java:23)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	... 1 more

二  解决方案:

    空指针错误主要是有变量没有交给spring管理注入所以,针对这样的问题,首先,可以打断点进行debug一次,寻找值为null的变量,进行修改.在使用spring管理quartz的情况下,出现空指针异常,则解决方案:

    1.可尝试在Scheduler中自定义JobFactory

@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {

	@Autowired
	private AutowireCapableBeanFactory capableBeanFactory;

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle)
			throws Exception {
		Object jobInstance = super.createJobInstance(bundle);
		capableBeanFactory.autowireBean(jobInstance);
		return jobInstance;
	}

}

    并且在application.xml的配置文件中配置:

	
		
		
			
				
			
		
	

2 可以尝试:

    方法1. 从Spring的ApplicationContext获取JobEndConference对象。

    方法2. 取消JobEndConference的本身和参数的注解,用new初始化。

    来源于:https://www.cnblogs.com/yoyotl/p/5624268.html


你可能感兴趣的:(debug)