0

I am getting the following error for a Scheduled Apex Job and don't know how to fix it (not a developer, just adapting some existing code). The aim is to update a field every night to trigger workflows, etc. I think the error could be around the field I want to update (as there are currently some records without a value in this field), but I could be wrong as the error mentions Object.

I've found similar queries but haven't figured out how to adapt answers for my specific case.

The full error is:

Scheduler: failed to execute scheduled job: jobId: 7071X000002o0om, class: common.apex.async.AsyncApexJobObject, reason: Attempt to de-reference a null object

Any suggestions of what to do / how to tweak the code greatly appreciated!

global class checkYoungPersonAllocations Implements Schedulable {

global void execute(SchedulableContext sc) {
    checkMyAllocations();
}

public void checkMyAllocations() {

    List<Young_Person_Allocation__c> affected = [SELECT Id, Name, Trigger_Field__c, Trip_Start_Date__c
                                 FROM Young_Person_Allocation__c
                                 WHERE Trip_Start_Date__c >= TODAY];

   for(Young_Person_Allocation__c thisAllocation : affected) {
       thisAllocation.Trigger_Field__c = thisAllocation.Trigger_Field__c + 1;
   }

  update affected;
}}
2

What I feel is, in your for loop, Trigger_Field__c is null. Null + 1 gives you NULL ptr exception. You have to do null check and then proceed.

global class checkYoungPersonAllocations Implements Schedulable {

global void execute(SchedulableContext sc) {
    checkMyAllocations();
}

public void checkMyAllocations() {

    List<Young_Person_Allocation__c> affected = [SELECT Id, Name, Trigger_Field__c, Trip_Start_Date__c
                                 FROM Young_Person_Allocation__c
                                 WHERE Trip_Start_Date__c >= TODAY];

   for(Young_Person_Allocation__c thisAllocation : affected) {
       if(thisAllocation.Trigger_Field__c!= null){
            thisAllocation.Trigger_Field__c = thisAllocation.Trigger_Field__c + 1;
       }else{
            thisAllocation.Trigger_Field__c =  1;

       } 
   }

  update affected;
}}
  • Thank you for replying so quickly. I suspected it might be that field but wasn't sure how to correct. I'll give that a go right now. – Brumps Feb 8 at 12:37
  • It worked perfectly @pranayjaiswal. Thank you so much! – Brumps Feb 8 at 13:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.