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
Bryan GordonBryan Gordon 

Map.get returning Null object

I have a trigger on a custom object (before insert and before update) that is looking at the Account object.  The custom object GWU40__c has an email address which is looking at the Account.PersonEmail (person-enabled accounts in Salesforce).

Every time it executes this line:
g.Nominee_Profile_ID__c = AccountMapNominees.get(g.Nominee_Email__c).FIMS_ID__c

it tells me de-referencing a null object (i'm sure it's becasue there isn't an Account with that email address).  
How can I check to see if the Map.get() is returning null?   

-------------------------------
trigger X_GWU40_LookupProfileIDs on GWU40__c (before insert,before update) {
    Set<String> noms = new Set<String>(); //Dedupe the email addresses in case of multiple nominations for the same person (email address)
    
    for(GWU40__c recordsToProcess : Trigger.New)
    {
        if(recordsToProcess.Nominee_EMail__c != null) {
            noms.add(recordsToProcess.Nominee_Email__c);
        }
        
    }
    
    Map<String,Account> AccountMapNominees = New Map<String,Account>();
        For(Account a : [SELECT PersonEmail,Name,FIMS_ID__c from Account where PersonEmail IN: noms]){
            AccountMapNominees.put(a.PersonEmail,a);
        }
      IF((!AccountMapNominees.isEmpty()) && (AccountMapNominees.size() > 0)){

      FOR(GWU40__c g : Trigger.New)
      {
          if (g.Nominee_Email__c != null)
          {            
             g.Nominee_Profile_ID__c = AccountMapNominees.get(g.Nominee_Email__c).FIMS_ID__c;                                 
          }
      }
     } //if 
   
}

------------------------------------
Best Answer chosen by Bryan Gordon
Tugce SirinTugce Sirin
You can do null control before accessing FIMS_ID__c field. It should look like this;
 
if(AccountMapNominees.get(g.Nominee_Email__c) != null) g.Nominee_Profile_ID__c = AccountMapNominees.get(g.Nominee_Email__c).FIMS_ID__c;

Let me know if this helps.

All Answers

Tugce SirinTugce Sirin
You can do null control before accessing FIMS_ID__c field. It should look like this;
 
if(AccountMapNominees.get(g.Nominee_Email__c) != null) g.Nominee_Profile_ID__c = AccountMapNominees.get(g.Nominee_Email__c).FIMS_ID__c;

Let me know if this helps.
This was selected as the best answer
Bryan GordonBryan Gordon
That worked.  Thank you.