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
UrvikUrvik 

Attempt to de-reference a null object:

I am able to save the trigger but getting the following error while creating / editing the record...

 

System.NullPointerException: Attempt to de-reference a null object: Trigger.ComplTrigger: line 34, column 1

 

see my trigger below..

 

trigger ComplTrigger on Complaint_Inquiries__c (before insert, before update) {

 
   Set<id> AccIds = new Set<id>();
   
    for (Complaint_Inquiries__c c : Trigger.new)
        AccIds.add(c.Account__c);
       
  
    Map<id, Account> AccIDmap = new Map<id, Account>([Select Name, BillingStreet,BillingCity, BillingState, BillingPostalCode from Account Where Id in :AccIds]);     
   
    for (Complaint_Inquiries__c Compl : Trigger.new){
        if(Compl.Copy_From_Account__c == true){
        Compl.Business_Name__c      = AccIDmap.get(Compl.Account__c).Name;
        Compl.Business_Address__c   = AccIDmap.get(Compl.Account__c).BillingStreet;
        Compl.Business_City__c         = AccIDmap.get(Compl.Account__c).BillingCity;
        Compl.Business_State__c       = AccIDmap.get(Compl.Account__c).BillingState;
        Compl.Business_Zip__c           = AccIDmap.get(Compl.Account__c).BillingPostalCode;
        Compl.Copy_From_Account__c = false;
        }
        }
        
        
        Set<id> ContIds = new Set<id>();
   
    for (Complaint_Inquiries__c c : Trigger.new)
        ContIds.add(c.Contact__c);
       
  
    Map<id, Contact> ContIDmap = new Map<id, Contact>([Select FirstName from Contact Where Id in :ContIds]);     
   
    for (Complaint_Inquiries__c Compl : Trigger.new){
        if(Compl.Copy_From_Contact__c == true){
        Compl.Business_Contact_First_Name__c = ContIDmap.get(Compl.Contact__c).FirstName;
        Compl.Copy_From_Contact__c = false;
        }
        }
        
        
        

}

 Please help

levaleva

Looks like you are pulling a null from ContIDmap. When it tries to get FirstName on null contact the error is thrown. You could probably by pass it with a null check :

 

if(ContIDmap.get(Compl.Contact__c) != null)
Compl.Business_Contact_First_Name__c = ContIDmap.get(Compl.Contact__c).FirstName;

 

Is Contact__c a required field on  Complaint_Inquiries__c?

UrvikUrvik
I am able to bypass the check using your solution but its not pulling data from contact.
It's an optional but lookup and it is required in contact object.
levaleva

Just for records without Contact__c filled out or all of them? If there is no contact reference in data there is no way to get the first name