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
Preeti ShettyPreeti Shetty 

System.NullPointerException: Attempt to de-reference a null object contact id

Hi All ,

I am getting the following error while deploying a trigger
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ActivityTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.ActivityTriggerHandler.getContactIds: line 17, column 1 Class.ActivityTriggerHandler.updateCallCount: line 7, column 1 Trigger.ActivityTrigger: line 3, column 1: []
Stack Trace: Class.TestUpdateAccountEngScore.myUnitTest: line 61, column 1

This my trigger handler -
public class ActivityTriggerHandler {
    private final String SUB_CATEGORY_CALL = 'Call';
    private final String SUB_CATEGORY_UPDATE = 'Update';
    private final String CONTACT_API = 'Contact';
    
    public void updateCallCount(List<Task> taskList) {
        Set<Id> contactIdSet = getContactIds(taskList);
        List<Contact> contactList = [SELECT Call_Count__c FROM Contact WHERE Id IN :contactIdSet];
        updateContacts(contactList);
    }
    
    private Set<Id> getContactIds(List<Task> taskList) {
        Set<Id> contactIdSet = new Set<Id>();
        for(Task t : taskList) {
            if(t.Sub_Category__c == SUB_CATEGORY_CALL | t.Sub_Category__c == SUB_CATEGORY_UPDATE) {
                Id whoId = t.WhoId;
                if(whoId.getSObjectType().getDescribe().getName() == CONTACT_API)
                    contactIdSet.add(whoId);
            }
        }
        return contactIdSet;
    }
    
    private void updateContacts(List<Contact> contactList) {
        for(Contact con : contactList) {
            if(con.Call_Count__c == NULL || con.Call_Count__c == 0)
                con.Call_Count__c = 1;
            else if(con.Call_Count__c >= 1)
                con.Call_Count__c = con.Call_Count__c + 1;
        }
        update contactList;
    }
}
and this is my trigger
trigger ActivityTrigger on Task (after insert, after update) {
    ActivityTriggerHandler handler = new ActivityTriggerHandler();
    handler.updateCallCount(Trigger.New);
}
Please help me resolve this.


 
Best Answer chosen by Preeti Shetty
Arunkumar RArunkumar R
Hi Preeti,

Could you try the below updated code,
 
public class ActivityTriggerHandler {
    private final String SUB_CATEGORY_CALL = 'Call';
    private final String SUB_CATEGORY_UPDATE = 'Update';
    private final String CONTACT_API = 'Contact';
    
    public void updateCallCount(List<Task> taskList) {
        Set<Id> contactIdSet = getContactIds(taskList);
        List<Contact> contactList = [SELECT Call_Count__c FROM Contact WHERE Id IN :contactIdSet];
        updateContacts(contactList);
    }
    
    private Set<Id> getContactIds(List<Task> taskList) {
        Set<Id> contactIdSet = new Set<Id>();
        for(Task t : taskList) {
            if(t.Sub_Category__c != null && ( t.Sub_Category__c == SUB_CATEGORY_CALL || t.Sub_Category__c == SUB_CATEGORY_UPDATE)) {
                
				if(t.WhoId !=null )
				{
					Id whoId = t.WhoId;
					if(whoId.getSObjectType().getDescribe().getName() == CONTACT_API)
					contactIdSet.add(whoId);
				}
            }
        }
        return contactIdSet;
    }
    
    private void updateContacts(List<Contact> contactList) {
        for(Contact con : contactList) {
            if(con.Call_Count__c == NULL || con.Call_Count__c == 0)
                con.Call_Count__c = 1;
            else if(con.Call_Count__c >= 1)
                con.Call_Count__c = con.Call_Count__c + 1;
        }
        update contactList;
    }
}


 

All Answers

Arunkumar RArunkumar R
Hi,

You can create task without whatid/whoid. Since you are referring whoid in your code. It may be chance of null. I have added certain null condition to avoid errors. Try the below updated code and if this solutions works for your mark it as solution.
 
public class ActivityTriggerHandler {
    private final String SUB_CATEGORY_CALL = 'Call';
    private final String SUB_CATEGORY_UPDATE = 'Update';
    private final String CONTACT_API = 'Contact';
    
    public void updateCallCount(List<Task> taskList) {
        Set<Id> contactIdSet = getContactIds(taskList);
        List<Contact> contactList = [SELECT Call_Count__c FROM Contact WHERE Id IN :contactIdSet];
        updateContacts(contactList);
    }
    
    private Set<Id> getContactIds(List<Task> taskList) {
        Set<Id> contactIdSet = new Set<Id>();
        for(Task t : taskList) {
            if(t.Sub_Category__c != null && ( t.Sub_Category__c == 'SUB_CATEGORY_CALL' || t.Sub_Category__c == 'SUB_CATEGORY_UPDATE')) {
                
				if(t.WhoId !=null )
				{
					Id whoId = t.WhoId;
					if(whoId.getSObjectType().getDescribe().getName() == CONTACT_API)
					contactIdSet.add(whoId);
				}
            }
        }
        return contactIdSet;
    }
    
    private void updateContacts(List<Contact> contactList) {
        for(Contact con : contactList) {
            if(con.Call_Count__c == NULL || con.Call_Count__c == 0)
                con.Call_Count__c = 1;
            else if(con.Call_Count__c >= 1)
                con.Call_Count__c = con.Call_Count__c + 1;
        }
        update contactList;
    }
}

 
Preeti ShettyPreeti Shetty
Hi Arunkumar,

I want the code to update the call count field in contact to increase by 1 whn task subject category is call or update. The above code is not performing any action.
Arunkumar RArunkumar R
Hi Preeti,

Could you try the below updated code,
 
public class ActivityTriggerHandler {
    private final String SUB_CATEGORY_CALL = 'Call';
    private final String SUB_CATEGORY_UPDATE = 'Update';
    private final String CONTACT_API = 'Contact';
    
    public void updateCallCount(List<Task> taskList) {
        Set<Id> contactIdSet = getContactIds(taskList);
        List<Contact> contactList = [SELECT Call_Count__c FROM Contact WHERE Id IN :contactIdSet];
        updateContacts(contactList);
    }
    
    private Set<Id> getContactIds(List<Task> taskList) {
        Set<Id> contactIdSet = new Set<Id>();
        for(Task t : taskList) {
            if(t.Sub_Category__c != null && ( t.Sub_Category__c == SUB_CATEGORY_CALL || t.Sub_Category__c == SUB_CATEGORY_UPDATE)) {
                
				if(t.WhoId !=null )
				{
					Id whoId = t.WhoId;
					if(whoId.getSObjectType().getDescribe().getName() == CONTACT_API)
					contactIdSet.add(whoId);
				}
            }
        }
        return contactIdSet;
    }
    
    private void updateContacts(List<Contact> contactList) {
        for(Contact con : contactList) {
            if(con.Call_Count__c == NULL || con.Call_Count__c == 0)
                con.Call_Count__c = 1;
            else if(con.Call_Count__c >= 1)
                con.Call_Count__c = con.Call_Count__c + 1;
        }
        update contactList;
    }
}


 
This was selected as the best answer
Preeti ShettyPreeti Shetty
Thanks Arunkumar. Its working properly now.