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
rahul Shukla 37rahul Shukla 37 

OpportunityTrigger: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object ()

I am getting the Null pointer error while keeping Lat invoice generated field blank.
Below is my code:
public class OpportunityTriggerEventHelper {
    
    public static void updateBaseRateOnOpp(Map<Id, Opportunity> oldDRObjMap, List<Opportunity> newDRObjList) {
        //if(checkRecursive.runOnce()){
            
            set<string> frequency = new set<string>();
            set<string> category = new set<String>();
            Set<Id> oppId = new Set<Id>();
            Map<string,Daily_Rate__c> maprate = new Map<string,Daily_Rate__c>(); 
            
            for(Opportunity d : newDRObjList){
                
                if(oldDRObjMap != null && oldDRObjMap.get(d.Id).cfg_LastInvGenDate__c !=d.cfg_LastInvGenDate__c) {
                    category.add(d.cfg_ChildAgeCat__c);
                    
                        oppId.add(d.Id);
                    }
                     }
            
            for(Opportunity opp : [SELECT Id, cfg_Enrollment__r.cfg_Attendance_Frequency__c FROM Opportunity WHERE ID IN :oppId]){
                frequency.add(opp.cfg_Enrollment__r.cfg_Attendance_Frequency__c);
            }
          
            list<Opportunity> oppLst = new list<Opportunity>();
            
            for(Opportunity opp : [SELECT Id, cfg_Enrollment__r.cfg_Attendance_Frequency__c, cfg_ChildAgeCat__c, cfg_LastInvGenDate__c FROM Opportunity WHERE ID IN :oppId]){
                
                for(Daily_Rate__c drObj : [SELECT Id, Attendance_frequency__c, Child_Category__c, Rate__c, Start_Date__c, End_Date__c 
                                           FROM Daily_Rate__c 
                                           WHERE Attendance_frequency__c = :opp.cfg_Enrollment__r.cfg_Attendance_Frequency__c AND Child_Category__c = :opp.cfg_ChildAgeCat__c
                                           AND Start_Date__c <= :opp.cfg_LastInvGenDate__c.addDays(7) AND End_Date__c >= :opp.cfg_LastInvGenDate__c.addDays(7)]){
                                               maprate.put(drObj.Attendance_frequency__c +''+drObj.Child_Category__c, drObj);   
                                           }
            }
            
            
            for(Opportunity opp : [SELECT Id, cfg_Enrollment__r.cfg_Attendance_Frequency__c, cfg_ChildAgeCat__c, cfg_LastInvGenDate__c FROM Opportunity WHERE ID IN :oppId]){
                if(maprate.containsKey(opp.cfg_Enrollment__r.cfg_Attendance_Frequency__c +''+ opp.cfg_ChildAgeCat__c)){
                    
                    if(maprate.get(opp.cfg_Enrollment__r.cfg_Attendance_Frequency__c +''+ opp.cfg_ChildAgeCat__c).Start_Date__c <= opp.cfg_LastInvGenDate__c.addDays(7) && maprate.get(opp.cfg_Enrollment__r.cfg_Attendance_Frequency__c +''+ opp.cfg_ChildAgeCat__c).End_Date__c >= opp.cfg_LastInvGenDate__c.addDays(7))
                        
                        opp.Base_Rate__c = maprate.get(opp.cfg_Enrollment__r.cfg_Attendance_Frequency__c +''+ opp.cfg_ChildAgeCat__c).Rate__c;
                       
                     
                        }
                oppLst.add(opp);
            }
            
            if(!oppLst.isEmpty()){
                update oppLst;    
            }
            
        //}
    }
}
AnudeepAnudeep (Salesforce Developers) 
This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.

The solution is to make sure the Object and/or the Attribute to be used is not null. In this example, the code needs to be modified as described in the documentation below

NullPointerException de-reference a null object in Apex code trigger

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
sachinarorasfsachinarorasf
Hi Rahul,
I can see in your code that there is no null check and when you tried to retrieve the null value then the code throw the same error.
Please check the list or set which you are going to use is not null and make sure that the size of the list or set is more than zero.
Please go through the following link for the best practices:
https://developer.salesforce.com/blogs/2020/09/what-can-null-do-to-me.html
https://www.mirketa.com/salesforce-development-best-practices/

I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com  (http://www.sachinsf.com)