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
SabrentSabrent 

Strange - Attempt to dereference null object error

I have a strange thing happening here.

 

I have this working otherwise, however for one condition -

 

Whenever the Account_Division__c is null (i.e not entered in the system), this logic fails to even enter the Else loop thus giving me the Attempt to dereference a null object error.

 

Any ideas why ??  

 

 

List <Employee__C> empList = [SELECT ..........];

for(Employee__c emp : empList){
	        	
	system.debug('Check the vlaue of Branch Email: ' +emp.Branch__r.Branch_Email__c);
	system.debug('Check the Kelly Division: ' +emp.Account_Division__c);

if(emp.Account_Division__c.contains('DAO')) {
	            	
  emp.Email_Branch__c = CT_EmailFields__c.getInstance().Mailbox__c;
	            
} else if(!emp.Account_Division__c.contains('DAO') || emp.Account_Division__c==null){ 

  emp.Email_Branch__c = emp.Branch__r.Branch_Email__c;

}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

It's because you're not doing a null check until the else if... and even then it's in the wrong order. Try this:

 

List <Employee__C> empList = [SELECT ..........];

for(Employee__c emp : empList){
    
    system.debug('Check the vlaue of Branch Email: ' +emp.Branch__r.Branch_Email__c);
    system.debug('Check the Kelly Division: ' +emp.Account_Division__c);
    
    if(emp.Account_Division__c != null && emp.Account_Division__c.contains('DAO'))
    {        
        emp.Email_Branch__c = CT_EmailFields__c.getInstance().Mailbox__c;        
    }
    else
    {
        emp.Email_Branch__c = emp.Branch__r.Branch_Email__c;        
    }    
}

 

All Answers

Sean TanSean Tan

It's because you're not doing a null check until the else if... and even then it's in the wrong order. Try this:

 

List <Employee__C> empList = [SELECT ..........];

for(Employee__c emp : empList){
    
    system.debug('Check the vlaue of Branch Email: ' +emp.Branch__r.Branch_Email__c);
    system.debug('Check the Kelly Division: ' +emp.Account_Division__c);
    
    if(emp.Account_Division__c != null && emp.Account_Division__c.contains('DAO'))
    {        
        emp.Email_Branch__c = CT_EmailFields__c.getInstance().Mailbox__c;        
    }
    else
    {
        emp.Email_Branch__c = emp.Branch__r.Branch_Email__c;        
    }    
}

 

This was selected as the best answer
m.elmoussaouim.elmoussaoui

Just check first if the emp.Account_Division__c is diffrent from null.

 

if(emp.Account_Division__c != null && emp.Account_Division__c.contains('DAO')) 
{
   .....
}

Cheers

Kiran  KurellaKiran Kurella

Hi Rov,

 

It looks like you want to assign Email_Branch__c field value with emp.Branch__r.Branch_Email__c, if the Account Division is null. If it is the case, then try the following code:

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

 

List<Employee__C> empList = [SELECT ..........];

for(Employee__c emp : empList){
        	
	system.debug('Check the vlaue of Branch Email: ' +emp.Branch__r.Branch_Email__c);
	system.debug('Check the Kelly Division: ' +emp.Account_Division__c);

	if(emp.Account_Division__c == null || !emp.Account_Division__c.contains('DAO')) {
	            	
  		emp.Email_Branch__c = emp.Branch__r.Branch_Email__c;
	            
	} else if(!emp.Account_Division__c.contains('DAO') || ){ 

  		emp.Email_Branch__c = CT_EmailFields__c.getInstance().Mailbox__c;
	}

}

 

 

 

SabrentSabrent

Thanks for al lyou r responses . Much appreciated, .