+ Start a Discussion
Faizan Ali 24Faizan Ali 24 

Could anyone help me with my trigger please - System.NullPointerException

I am trying to fire this trigger:

trigger Autofill on Event (before insert, before update) {  
        for (Event e : Trigger.new) 
            boolean flag = (e.Appointment_with__c==null)?true:false;
            String [] str = e.Appointment_with__c.split(' ');
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];              
            e.Staff__c = cont[0].Id;


But I keep getting this error and I don't know how to fix it at all:

Autofill: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Autofill: line 10, column 1
ShirishaShirisha (Salesforce Developers) 
Hi Faizan,


Seeme like the list "cont" is null/doesn't have any records due to which you are getting the error.

I would suggest you to change the code as suggested in the article (https://help.salesforce.com/articleView?id=000327918&type=1&mode=1) to get the user friendly error message.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri