Search code examples
javanullpointerexceptionquartz-scheduler

NullPointerException using Quartz rescheduler function


I am new to Quartz API and currently using quartz version 2.2.x to create a scheduler class object in java to support my school project but encountered null pointer exception with scheduler.rescheduleJob(trigger.getKey(), cronTrigger); at rescheduleCron function. I am trying to setup a function to accept cron expression to reschedule quartz jobs and have no idea what I am doing wrong. Please help me to rectify this error.

public class quartzScheduler {

public JobDetail job;
public Trigger trigger;
public Scheduler scheduler;

public void quartzScheduler() throws SchedulerException,InterruptedException
{
    job = JobBuilder.newJob(quartzJob.class)
            .withIdentity("quartzJob", "group1").build();

    trigger = TriggerBuilder
            .newTrigger()
            .withIdentity("quartzTrigger", "group1")
                            .startAt(futureDate(1, IntervalUnit.HOUR))
            .withSchedule(
                    SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInHours(1).repeatForever())
            .build();

    scheduler.start();
    scheduler.scheduleJob(job, trigger);
}

    public void rescheduleCron(String cronExpression) throws SchedulerException
    {
        Trigger cronTrigger = TriggerBuilder
            .newTrigger()
            .withIdentity("quartzTrigger", "group1")
            .withSchedule(cronSchedule(cronExpression))
            .build();

        scheduler.rescheduleJob(trigger.getKey(), cronTrigger);    
    }


public static void main(String[] args) 
{
    quartzScheduler sch= new quartzScheduler(); 
    try {
        sch.rescheduleCron("0 0 * * * ?");
    } catch (SchedulerException ex) {
        Logger.getLogger(quartzScheduler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Solution

  • In rescheduleCron(), your trigger has not been initialized. It is set to null and when you try to call .getKey() on it, you're getting a NullPointerException.

    I see what you're doing... I think you're trying to use quartzScheduler() as a constructor which is why you're expecting the trigger to be initialized. Very small change to fix this: remove the return value from that function. Java constructors do not have return values.

    It should look like this:

    public class QuartzScheduler {
    
        public JobDetail job;
        public Trigger trigger;
        public Scheduler scheduler;
    
        public QuartzScheduler() throws SchedulerException, InterruptedException {
            job = JobBuilder.newJob(quartzJob.class)
                    .withIdentity("quartzJob", "group1").build();
    
            trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("quartzTrigger", "group1")
                    .startAt(futureDate(1, IntervalUnit.HOUR))
                    .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInHours(1).repeatForever())
                    .build();
            scheduler = new Scheduler(); //maybe do something else here.
    
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
        }
    
        public void rescheduleCron(String cronExpression) throws SchedulerException {
            Trigger cronTrigger = TriggerBuilder
                .newTrigger()
                .withIdentity("quartzTrigger", "group1")
                .withSchedule(cronSchedule(cronExpression))
                .build();
    
            scheduler.rescheduleJob(trigger.getKey(), cronTrigger);    
        }
    }
    
    public static void main(String[] args) {
        QuartzScheduler sch= new QuartzScheduler(); 
        try {
            sch.rescheduleCron("0 0 * * * ?");
        } catch (SchedulerException ex) {
            Logger.getLogger(quartzScheduler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    What I've changed:

    • Capitalized your class name (by convention)
    • Removed the return values and the exceptions from the constructor
    • moved the main() call to outside of the class. The main() call uses the class, it is not part of it