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
Hermann OuréHermann Ouré 

How to fix System Null Pointer Exception

Hello,
I have created a trigger to update a field anytime a specific task is created.
But when trying to add another type of Task I have the following error
User-added imageThis is the line 11 causing the error 
if(t.whatId != null && t.callDisposition.containsIgnoreCase('aircall')) {

How can I fix the nullPointer exception:
Thanks.

Here is the trigger 
 

trigger AircallTaskTrigger on Task (after insert, after update) {

        List<Case> lstCase = new List<Case>();
     
     	//Get 'Case Concern' RecordType Name
        Id caseConcernRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get(label.Param_Case_Concern).getRecordTypeId();

        Map<Id, Case> caseWithAircallTaskMap = new Map<Id, Case>([SELECT Id, First_Aircall_Logged__c FROM Case WHERE RecordTypeId =:caseConcernRecordTypeId]);                           
        for(Task t : Trigger.New) { 
           
            if(t.whatId != null && t.callDisposition.containsIgnoreCase('aircall')) {
               System.debug('#### Aircall debug ' + t.whatId != null && t.callDisposition.containsIgnoreCase('aircall'));
                
                Case c = New Case();
                if(caseWithAircallTaskMap != null && caseWithAircallTaskMap.containsKey(t.whatId)){
                
                    c = caseWithAircallTaskMap.get(t.whatId);
                    if(c.first_Aircall_Logged__c == null){
                        c.first_Aircall_Logged__c = t.createdDate;
                        if(t.WhatId == c.Id) {
                            lstCase.add(c); 
                        }              
                    } 
                } 
            }     
        }
     
        if (lstCase.size() > 0) {
        	update lstCase;
     	}  
  }

Here is the Test Class
 
@isTest
public class AircallTaskTriggerTest {
    @isTest static void testAircallDateUpdate() {
        
        Contact con = new Contact (FirstName = 'First Name',LastName = 'Test');
        insert con;
        
        Case c = new Case(Status = 'New',ContactId = con.Id,Phone_Number__c = '123456789');
        insert c;
               
        Task t = new Task(Subject = 'Test', WhatId = c.Id, CreatedDate = System.today(), callDisposition = 'aircall');
        insert t;
        
        c.Id = t.WhatId;
        c.First_Aircall_Logged__c = t.CreatedDate;
        System.assertEquals(t.CreatedDate,  c.First_Aircall_Logged__c);
        update c;
       
    }
    
}

 
Best Answer chosen by Hermann Ouré
Abhishek BansalAbhishek Bansal
Changed the line that is throwing error as below:

if(t.whatId != null && t.callDisposition != null && t.callDisposition.containsIgnoreCase('aircall')) {