3

I'm working on a task in which I need to save job status in database but when I tried to do this it gives me java.lang.nullpointerexception. I think it happen because whenever I tried to select/save/update any record from the database only at that time it gives me error similar to this.

Here is my code

public class PostFBJob implements Job {

    private SchedulerService schedulerService;

    private SubCampaignService subCampaignService;

    @SuppressWarnings("unchecked")
    public void execute(JobExecutionContext context) throws JobExecutionException {

        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        schedulerService = (SchedulerService) jobDataMap.get("schedulerService");

        SubCampaign subCampaign = (SubCampaign) jobDataMap.get("subCampaign");

        if (prStreamItem.getName().equalsIgnoreCase("Facebook") && StringUtils.isNotBlank(branch.getFbAccessToken())) {
            FacebookService facebookService = FacebookService.getSingleton();
            try {
                subCampaign.setStatus("Completed");
                subCampaign.setMessage("Completed");

                subCampaignService.updateSubCampaign(subCampaign);

            } catch (Exception e) {
                log.error("", e);
            }
        }
    }
}

Exception

java.lang.NullPointerException
    at com.ace.Job.SubCampaignJob.execute(SubCampaignJob.java:147)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
Error: java.lang.NullPointerException

Please help me to solve this issue. I'm new to Spring and Quartz.

Thanks in advance

Ace
  • 63
  • 7
  • Please also provide whole exception in question . – Bhuwan Prasad Upadhyay Nov 24 '15 at 10:24
  • 2
    NullPointerException is one of the easiest problems to debug: the stack trace tells you the file and line number at which it occurs. A quick spin in a debugger will show you which reference has not been initialized properly. Can't tell from the code you posted. I see no database code here; updateSubCampaign is my best guess. – duffymo Nov 24 '15 at 10:24
  • @developerbhuwan Thanks for replying. I only get `java.lang.NullPointerException` at this line `subCampaignService.updateSubCampaign(subCampaign);` – Ace Nov 24 '15 at 10:26
  • @duffymo Thanks for replying but sorry to say I'm not able to get any other detail. it's just throwing an error when I tried to get/update any record from the database... – Ace Nov 24 '15 at 10:28
  • 1
    Yes you are. Use a debugger. IntelliJ is the best IDE on the market, and they have a community version. Go get it. Debugging is a key skill that you have to develop in order to be a successful programmer. – duffymo Nov 24 '15 at 10:30
  • @duffymo Thanks for your suggestion. I'll work on this but can you help me with this right now as it'l take a lot of time to go for your idea now – Ace Nov 24 '15 at 10:33
  • 1
    Nope. You posted this question 47 minutes ago as I write this. You would have found this and moved on already if you had a debugger and some idea of how to use it. Even adding println output for each reference before you used it would have revealed this faster than asking at SO. – duffymo Nov 24 '15 at 11:06
  • 1
    @KatjaChristiansen This question is totally different from the `nullpointerexception` because there is an error occured due to quartz controller which is not allowing me to access service layer from in it – Ace Nov 26 '15 at 06:07

2 Answers2

4

This exception due to null object of subCampaignService. Injection work only those objects which are managed by spring.

To scan your all packages where you want to use Service.

  1. Annotation based configuration

    @ComponentScan({"com.ace"})

  2. XML based configuration

    <context:component-scan base-package="com.ace"/>

And also check null before update like below:

try {
    subCampaign.setStatus("Completed");
    subCampaign.setMessage("Completed");
    if(subCampaign != null && subCampaignService != null) //Check is not null to subCampaign before update
    subCampaignService.updateSubCampaign(subCampaign);
} catch (Exception e) {
     log.error("", e);
}
Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33
2

I think the answer you are looking for is here

Please check below link

Correct way to persist Quartz triggers in database

And you can also check this link

Using Hibernate session with quartz

Community
  • 1
  • 1
Luffy
  • 1,317
  • 1
  • 19
  • 41
  • I don't understand why anyone would prefer Quartz over the built in Executor. It feels like an unnecessary dependency. – duffymo Nov 24 '15 at 12:49
  • @duffymo Sorry, but I'm not able to understand what you want to say here. I answered this question because I think this is what he was looking for – Luffy Nov 24 '15 at 17:34
  • 1
    This comment is not directed at you or criticism of your answer. It's more of an observation than anything else. I think Quartz is unnecessary since JDK7 and the concurrent package. – duffymo Nov 24 '15 at 18:05