+ Start a Discussion
Sachin K 13Sachin K 13 

Attempt to dereference a null object

Dear friends, 
Please help me this: 
I have trigger code on Task object ( after insert / after update) which is going to update recent due date of completed F2F activity on opportunity custom date field. And there is also a after update trigger code on opportunity which updates the custom field on account that  is just updated by task trigger. My question is : I am getting attempt to dereference a null object error. How to solve this. Appreciate if any pointers to achieve this requirement.
Ajay K DubediAjay K Dubedi
Hi Sachin,
It is the problem of the null check, just see if you are fetching only those opportunities that have a account associated with them in the Task trigger because there can be a possibility that you are updating an opportunity that is not associated with any account. It would be better if you can provide your code.
Thanks 
Ajay
Sachin K 13Sachin K 13
Hi Ajay,
Here is the code ,
// On Task we are using Account_Group_Task__c custom formula field which brings the Parent Account name on Task with respect to WhatId. So that when we query based on Account Group we will get all tasks of Parent account and its child accounts

//Task Trigger
if(trigger.isAfter)
    {
        if(trigger.isInsert || trigger.isUpdate)
        { 
             RelatedTask.method1(trigger.new);
         }
}
//Task Class: RelatedTask
public static void method1(List<Task> taskRecords){   
try{
      
        List<Task> listTsk=new List<Task>();
        List<Contact> listCont=new List<Contact>();
        List<String> tskGrpName=new List<String>();    
        List<Id> taskWhatId=new List<Id>();
        List<Task> accountTasks=new List<Task>();
       
        for(Task tsk:taskRecords)
        {
            String taskgr=tsk.Account_Group_Task__c;
            Id taskleadId=tsk.WhoId;
                                   
            // if(wId!=null && wId.startsWith('001') && !listTask.contains(tsk.WhatId))
            if(taskgr!=null)
            {   
                tskGrpName.add(tsk.Account_Group_Task__c);
            }             
           
             if(tsk.WhatId!=null)
             {
                 taskWhatId.add(tsk.WhatId);
             }
           
        }                                                   
		
      
listTsk= [SELECT Id, ActivityDate,WhatId,WhoId,Primary_Contact__c,Status,Meeting_Purpose__c,Type,Account_Name__c,
RecordTypeId,Account_Group_Task__c,Last_meeting_90_days__c,Next_meeting_90_days__c FROM Task Where Account_Group_Task__c IN : tskGrpName AND (RecordTypeId=:Account_Activity_RECORD_TYPE_ID OR RecordTypeId=:Opportunity_Activity_RECORD_TYPE_ID) AND (Meeting_Purpose__c='Executive' OR Meeting_Purpose__c='Operational')  AND Status='Completed' AND (Type='F2F' OR Type='Call') Order by ActivityDate];
              
        Map<Id, Account> accMaps=new Map<Id, Account>([Select Id,Last_Onsite_Activity_A_T__c,Last_Remote_Activity_A_T__c,Name from Account Where Id IN:taskWhatId]);
            
     
        Map<Id, Opportunity> oppMaps=new Map<Id, Opportunity>([Select Id,Last_Onsite_Activity_O_T__c,Last_Remote_Activity_O_T__c from Opportunity where Id IN:taskWhatId]);    
                
        Map<Id, Account> actn = new Map<Id, Account>();
        Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
     
        for(Task tk:listTsk)
        {   
            if(tk.Type=='F2F')
            {
               if(accId!=null)
                {  System.debug('Account Name is ' + accId.Name);
                        System.debug('Task Type ' +tk.Type);
                   		 if(tk.WhatId==accId.Id)
                			{
                   				accId.Last_Onsite_Activity_A_T__c=tk.ActivityDate;
                			}
                 }
                else if(oppId!=null)
                 {
                     if(tk.WhatId==oppId.Id){
                     oppId.Last_Onsite_Activity_O_T__c=tk.ActivityDate;
                 } 
					}
            }
            if(tk.Type=='Call')
            {
             if(accId!=null)
                {  System.debug('Account Name is ' + accId.Name);
                      System.debug('Task Type ' +tk.Type);
                   		 if(tk.WhatId==accId.Id)
                			{
                                accId.Last_Remote_Activity_A_T__c=tk.ActivityDate;
                			}                            
                    }
                else if(oppId!=null)
                {
                    if(tk.WhatId==oppId.Id){
                     oppId.Last_Remote_Activity_O_T__c=tk.ActivityDate;
                                System.debug('Opp Last Remote Activity ' +  oppId.Last_Remote_Activity_O_T__c);
                 }
                }
            }
             }
        update oppMaps.values();
        update accMaps.values();
        
   
    } catch(DmlException de)
    {
        System.debug('Error While Executing '+ de.getLineNumber());
    }
}
----------------------------------------------------------------------------------------------------------------
//Opportunity Trigger
if(trigger.isAfter)
    {    
        if(trigger.isUpdate)
        {
          CustomOpportunity.findRecentActivityDate(trigger.new);  
        }
    }


//Opportunity Class : CustomOpportunity
public static void findRecentActivityDate(List<Opportunity> opptys)
    { 
  try{
        List<Opportunity> listOpp=new List<Opportunity>();
        Set<Id> oppIds=new Set<Id>();
        Set<Id> accIds=new Set<Id>();
        List<String> accountGroupId=new List<String>();
        for(Opportunity ops:opptys)
        {
            oppIds.add(ops.Id);
            accIds.add(ops.AccountId);
            accountGroupId.add(ops.Account_Group_Id_Opp__c);
        }

        Map<Id, Opportunity> oppsOnsite=new Map<Id, Opportunity>([select Id,AccountId,Last_Onsite_Activity_O__c,Last_Remote_Activity_O_T__c from Opportunity where Account_Group_Id_Opp__c IN:accountGroupId Order by Last_Onsite_Activity_O__c Desc Limit 1]);

        Map<Id, Opportunity> oppsRemote=new Map<Id, Opportunity>([select Id,AccountId,Last_Onsite_Activity_O__c,Last_Remote_Activity_O_T__c from Opportunity where Account_Group_Id_Opp__c IN:accountGroupId Order By Last_Remote_Activity_O_T__c Desc Limit 1]);
       
 Map<Id, Account> accnts= new Map<Id, Account>([Select Id, Last_Onsite_Activity_A_O__c,Last_Remote_Activity_A_O__c from Account where Id IN:accIds]);
     
        for(Opportunity opp : oppsOnsite.values())
        {
	        Account accId=accnts.get(opp.AccountId);
            accId.Last_Onsite_Activity_A_O__c=opp.Last_Onsite_Activity_O__c;
        }

    	for(Opportunity opp1:oppsRemote.values())
        {
            Account accId1=accnts.get(opp1.AccountId);
            accId1.Last_Remote_Activity_A_O__c=opp1.Last_Remote_Activity_O_T__c;
        }
       update accnts.values();
     }
Catch(DMLException de)
        {
            System.debug('Error While Executing ' + de.getLineNumber());
        }
    }