function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jagadeesh AdaveniJagadeesh Adaveni 

execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.editGenerateLogbooks: line 55, column 1

Hi All,

I am getting following error in my trigger.
execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.editGenerateLogbooks: line 55, column 1

Could you please resolve it.
trigger editGenerateLogbooks on Log_Book__c (after update) {
    
    Set<id> set1 = new Set<id>();
    Set<String> weeks = new Set<String>(); 
    Set<String> subjects = new Set<String>();
    Set<String> students = new Set<String>();   
    
    List<Weeks__c> weekList = Weeks__c.getAll().values();
    List<Log_Book__c> lb = new List<Log_Book__c>();
    List<Log_Book__c> lbDelList = new List<Log_Book__c>();
    List<Log_Book__c> lstToUpdate = new List<Log_Book__c>();
    
    //Map for Custom setting object
    Map<Integer,weeks__c> weekMap = new Map<Integer,weeks__c>();
    List<Log_Book__c> lstToAdd = new List<Log_Book__c>();
    Set<Log_Book__c> setLog = new Set<Log_Book__c>();
    
    for(weeks__c week : weekList)
    {
        weekMap.put((Integer)week.Order__c,week);
         
    }
    for(Log_Book__c lbook : Trigger.old)
    {
        set1.add(lbook.Id);
    }
    lb=[select Abacus__c, Absent_Time_Stamp__c, Absent__c, Attended__c, Class_Scheduled_Date__c, Collected_Book__c, Comments__c, Do_Not_Promote__c, English__c, Level__c, Order__c, Reason__c, Id, Student__c, Subject__c, Subjects_Registered__c, Week__c, Week_Day__c, gk__c, math__c from log_book__c where id in: set1];
    for(Log_Book__c lbdelete : lb)
    {
        if(lbdelete.Class_Scheduled_Date__c == System.today())
        {
            lbDelList.add(lbdelete);
            system.debug('@@@@@@@@@@Deleted Record is@@@@@@'+lbDelList);
           
        }
    }
    for(Log_Book__c lbUpdate : Trigger.new)
    {
        
    
            weeks.add(lbUpdate.week__c);
            subjects.add(lbUpdate.Subject__c);
            students.add(lbUpdate.Student__c); 
   
    }
    
      for(Log_Book__c lbup : [select Abacus__c, Absent_Time_Stamp__c, Absent__c, Attended__c, Class_Scheduled_Date__c, Collected_Book__c, Comments__c, Do_Not_Promote__c, English__c, Level__c, Order__c, Reason__c, Id, Student__c, Subject__c, Subjects_Registered__c, Week__c, Week_Day__c, gk__c, math__c from log_book__c where subject__c IN: subjects AND Student__c IN: students AND week__c IN: weeks AND Class_Scheduled_Date__c >=: system.today() order by order__c])
      {
      
          Integer weekLevel = 0;     
          String[] weekLevels = lbup.Week__c.split('-');
          //will get only week number of week__c field of Log_Book__c object selected value will store in first index 
          weekLevel = Integer.valueof(weekLevels[0]);
   
       if(lbup.week__c != Trigger.oldMap.get(lbup.id).week__c || lbup.Class_Scheduled_Date__c >= System.Today())
       {
           for(Integer j=weekLevel;j<27;j++)
           {
            system.debug('++hiiiiiiiiiii+');
            lbup.Class_Scheduled_Date__c = lbup.Class_Scheduled_Date__c.addDays(7);
            //assigning custom setting value to week__c field based on selected week number fromlog_book__c object
            lbup.week__c = weekMap.get(weekLevel).name;
            system.debug('*****Jagadeesh********'+lbup.Class_Scheduled_Date__c + lbup.Level__c + lbup.Week__c);
            //lstToAdd.add(lbup);
            
            weekLevel++;
            
            }
            lstToAdd.add(lbup);
            weekLevel = 1;
        }
      }
    
    
    setLog.addAll(lstToAdd);
    lstToUpdate.addAll(setLog);
   
    update lstToUpdate;
}

Thanks
A.Jagadeesh
Bhanu MaheshBhanu Mahesh
Hi Jagadeesh,

You are getting Null Pointer Exception because of Trigger.oldMap.get(lbup.id).week__c.

From the query, you are getting a record which is not present in the Trigger.oldMap  so Trigger.oldMap.get(lbup.id) is null and you are accessing a field from it.

From the below query

[select Abacus__c, Absent_Time_Stamp__c, Absent__c, Attended__c,Class_Scheduled_Date__c, Collected_Book__c, Comments__c, Do_Not_Promote__c, English__c,Level__c, Order__c, Reason__c, Id, Student__c, Subject__c, Subjects_Registered__c, Week__c,Week_Day__c, gk__c, math__c from log_book__c where subject__c IN: subjects AND Student__c IN: students AND week__c IN: weeks AND Class_Scheduled_Date__c >=: system.today() order byorder__c]

So check your data, actually you are filtering on subject__c,Student__c, week__c but not with Id. So there may be chance of getting more records which are not present in Trigger.oldMap. Try filtering with Id's also or Check for null before accesing field. 
Trigger.oldMap.get(lbup.id) != null

Regards,
Bhanu Mahesh
Jagadeesh AdaveniJagadeesh Adaveni
If i remove that Trigger.oldMap.get(lbup.id).week__c then i am getting following error like

eScheduleLogBooks: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a04f0000005HApPAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, editGenerateLogbooks: maximum trigger depth exceeded Log_Book 
StephenKennyStephenKenny
Hi there,

The latest error is because you are causing a recurring update with line 78. if you wish to update the existng record, I suggest you move this logic into a before trigger and only use the after trigger if you need to upadate other records.

Please remember to mark this thread as solved with the answer that best helps you.

Regards
Stephen 
 
Swayam  AroraSwayam Arora
Hi Jagadeesh,
You can update the if statement as:-

if((Trigger.oldMap.containsKey(lbup.id) == true && lbup.week__c != Trigger.oldMap.get(lbup.id).week__c) || lbup.Class_Scheduled_Date__c >= System.Today())

To solve the Recursive updation, you can take help from the below link
http://www.infallibletechie.com/2012/08/recursive-triggers-in-salesforce.html

Let me know if you need further help

Please mark the answer as Best answer if it really helped. Closing the thread help others as well.