You need to sign in to do that
Don't have an account?

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 -
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.
Could you try the below updated code,
All Answers
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.
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.
Could you try the below updated code,