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
Patrick G. BrownPatrick G. Brown 

Need a little assistance with Null Pointer Exception

I have a relatively simple trigger that looks through Campaign Members, finds the associated Lead records and checks to see if the Current_Campaign__c field is null on the Lead.  If that field is not null, I throw an error.  I thought I'd used best practices, however, I'm getting a null pointer exception on this line: if(associatedLead2.Current_Campaign__c!=NULL).  The full error reads: checkCurrentCampaign: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.checkCurrentCampaign: line 13, column 1

Can anyone see what I may have done?  I've tried to edit this a dozen or so different ways to ensure I'm not pulling null records, but I'm still receiving an error.  Any assistance would be most appreciated.

My trigger:
 
trigger checkCurrentCampaign on CampaignMember (before insert) {
    Set<Id> leadIds = new Set<Id>();
    for (CampaignMember cml: Trigger.new){    
            leadIds.add(cml.LeadId);
        }
    
        Map<Id, Lead> LeadMap2 = new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status FROM Lead WHERE Id IN :leadIds]);
    
    //Loop through Campaign Members and get Associated Lead ID
        for (CampaignMember cml: Trigger.new){
            Lead associatedLead2 = LeadMap2.get(cml.LeadId);
            //Still getting a dereference null error    
                if(associatedLead2.Current_Campaign__c!=NULL)
                {
            cml.adderror('Campaign Members can only be in one Campaign at a time.  If you are receiving this error, you are attempting to add one or more Leads to multiple active Campaigns');
                }
        }   
    }

 
Pasan   EeriyagamaPasan Eeriyagama
I would suggest you could check if "associatedLead2"  is null before checking it's attribute is null.

For example,
//Loop through Campaign Members and get Associated Lead ID
    for (CampaignMember cml: Trigger.new){
        Lead associatedLead2 = LeadMap2.get(cml.LeadId);
        //Still getting a dereference null error    
        if (associatedLead2==null)
            cml.adderror('Lead was not found.');
        if(associatedLead2.Current_Campaign__c!=NULL)
        {
            cml.adderror('Campaign Members can only be in one Campaign at a time.  If you are receiving this error, you are attempting to add one or more Leads to multiple active Campaigns');
        }
    }


 
Patrick G. BrownPatrick G. Brown
Pasan, thanks so much for the idea.  I tried it, unfortunately, it didn't help.  I'm still receiving the same error.
Pasan   EeriyagamaPasan Eeriyagama
Sorry, the code is missing else if,
 
//Loop through Campaign Members and get Associated Lead ID
    for (CampaignMember cml: Trigger.new){
        Lead associatedLead2 = LeadMap2.get(cml.LeadId);
        //Still getting a dereference null error    
        if (associatedLead2==null)
            cml.adderror('Lead was not found.');
        else if(associatedLead2.Current_Campaign__c!=NULL)
        {
            cml.adderror('Campaign Members can only be in one Campaign at a time.  If you are receiving this error, you are attempting to add one or more Leads to multiple active Campaigns');
        }
    }

 
Gokula KrishnanGokula Krishnan
Hi Patrick,

Try this,

trigger checkCurrentCampaign on CampaignMember (before insert) {
    Set<Id> leadIds = new Set<Id>();
    for (CampaignMember cml: Trigger.new){    
            leadIds.add(cml.LeadId);
        }
    
    if(leadIds.size()>0){
    Map<Id, Lead> LeadMap2 = new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status FROM Lead WHERE Id IN :leadIds]);
    
    //Loop through Campaign Members and get Associated Lead ID
        for (CampaignMember cml: Trigger.new){
            Lead associatedLead2 = new Lead();
            if(LeadMap2.containskey(cml.LeadId)){
                associatedLead2 = LeadMap2.get(cml.LeadId);
                //Still getting a dereference null error    
                if(associatedLead2.Current_Campaign__c!=NULL)
                {
                cml.adderror('Campaign Members can only be in one Campaign at a time.  If you are receiving this error, you are attempting to add one or more Leads to multiple active Campaigns');
                }
            }
            
        }  
    }        
}    

If it helps you, please mark is as best answer, so it will be helpful for other developers.
Amit Chaudhary 8Amit Chaudhary 8
Try to add Null Check in You code. Use ContainsKey Method like below
trigger checkCurrentCampaign on CampaignMember (before insert) 
{
	Set<Id> leadIds = new Set<Id>();
	for (CampaignMember cml: Trigger.new)
	{
		leadIds.add(cml.LeadId);
	}
	Map<Id, Lead> LeadMap2 = new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status FROM Lead WHERE Id IN :leadIds]);

	for (CampaignMember cml: Trigger.new)
	{
		if(LeadMap2.containsKey(cml.LeadId))
		{
			Lead associatedLead2 = LeadMap2.get(cml.LeadId);
			if(associatedLead2.Current_Campaign__c != NULL)
			{
				cml.adderror('Campaign Members can only be in one Campaign at a time.  If you are receiving this error, you are attempting to add one or more Leads to multiple active Campaigns');
			}
		}		
	}   
}

Let us know if this will help you