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
Dman100Dman100 

NullPointerException: Attempt to de-reference a null object:

I am getting a NullPointerException: Attempt to de-reference a null object error on the following trigger code:
 
Code:
trigger ImportMDRDataTrigger on Lead (before update) {
 
 Lead[] leadList = [Select Id, MDR_Account__r.Enrollment__c, MDR_Account__r.MDR_School_I_D__c, MDR_Account__r.MDR_District_I_D__c from Lead WHERE MDR_Account__c != null and Id in :Trigger.new];

 Map<Id, Lead> leadMap = new Map<Id, Lead>(leadList);
 
 for (Lead l : Trigger.new) {
  if (l.MDR_Account__c == null)       
  continue;
  Lead the_lead = leadMap.get(l.Id);
     l.Enrollment__c = the_lead.MDR_Account__r.Enrollment__c;
     l.MDR_District_PID__c = the_lead.MDR_Account__r.MDR_District_I_D__c;
     l.MDR_School_PID__c = the_lead.MDR_Account__r.MDR_School_I_D__c;
  }
}

 
The trigger works if the MDR_Account__c field in the lead object is already populated and I run the update.  However, if I edit the lead and do the lookup on the MDR_Account__c field, the NullPointerException error is thrown.
 
Thanks for any help.
Best Answer chosen by Admin (Salesforce Developers) 
arnt72arnt72
sorry, misunderstood the question/situation. AT is right. His approach with the formular fields has also the advantage that the data is always up to date , even if the parent gets updated. The trigger will only pull actual data when the child is updated.

Message Edited by arnt72 on 10-13-2008 04:52 PM

Message Edited by arnt72 on 10-13-2008 04:55 PM

Message Edited by arnt72 on 10-13-2008 04:57 PM

All Answers

AkiTAkiT
Your leadList will be empty, if you update a Lead that has the MDR_Account__c empty. SOQL looks the old data in the database.

If your intention is to fill in the values from the parent for every Lead you could get the values to Lead detail page also by creating a formula field.




Dman100Dman100
How can I check if leadList is null or empty and then process if leadList is not null?
 
Code:
trigger ImportMDRDataTrigger on Lead (before update) {
 
 Lead[] leadList = [Select Id, MDR_Account__r.Enrollment__c, MDR_Account__r.MDR_School_I_D__c, MDR_Account__r.MDR_District_I_D__c from Lead WHERE MDR_Account__c != null and Id in :Trigger.new];

 if (leadList != null)
        {
        Map<Id, Lead> leadMap = new Map<Id, Lead>(leadList);
 
 for (Lead l : Trigger.new) {
  if (l.MDR_Account__c == null)       
  continue;
  Lead the_lead = leadMap.get(l.Id);
     l.Enrollment__c = the_lead.MDR_Account__r.Enrollment__c;
     l.MDR_District_PID__c = the_lead.MDR_Account__r.MDR_District_I_D__c;
     l.MDR_School_PID__c = the_lead.MDR_Account__r.MDR_School_I_D__c;
  }
        }
}

 
Thanks.
AkiTAkiT
YourList.isEmpty() method returns true if no values in the list.

But your SOQL query won't work as expected in this case. Instead you should look at querying MDR_Account__c directly, like:
Code:
Pseudo:

Set <Id> Ids of Lead.MDR_Account__c

Map <Id, Value> Select Id, Enrollment__c, ... from MDR_Account__c where Id in :Ids

For Lead l : Trigger.new
      Correlate l.MDR_Account__c to key in Map <Id, Value> and get values


 

arnt72arnt72
sorry, misunderstood the question/situation. AT is right. His approach with the formular fields has also the advantage that the data is always up to date , even if the parent gets updated. The trigger will only pull actual data when the child is updated.

Message Edited by arnt72 on 10-13-2008 04:52 PM

Message Edited by arnt72 on 10-13-2008 04:55 PM

Message Edited by arnt72 on 10-13-2008 04:57 PM
This was selected as the best answer