Happy New Year to everyone.

I started salesforce Learning and getting some error while writing the trigger. Actually I created on new field on Lead object with name of UpdatedByTrigger__c and this is checkbox.

this is my code:

trigger vld_Updt_By_Trigger on Lead (before update) {

    for(Lead leads:Trigger.New)
        Lead oldLeads = Trigger.OldMap.get(leads.Id);
        Boolean IsOldLeads = oldLeads.LeadSource.equals('Other');
        Boolean IsNewLeads = leads.LeadSource.equals('Other');          

        if(!IsOldLeads && IsNewLeads){
            leads.UpdatedByTrigger__c = true;


Code is working fine but I am updating lead record without having value in Lead source, I am getting

Error:Apex trigger vld_Updt_By_Trigger caused an unexpected exception, contact your administrator: vld_Updt_By_Trigger: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.vld_Updt_By_Trigger: line 8, column 1

can any one tell me what mistake I am doing here?

2 Answers 2


You can make the code null safe by simply using double equals comparison (==) instead of .equals, with the added benefit that it will become case-insensitive, which is probably more correct. Most of the time, you want to use ==, unless you have a compelling reason .equals is better, which should really only be if you need the casing to be exact.


Boolean IsOldLeads=oldLeads.LeadSource.equals('Other');
Boolean IsNewLeads=leads.LeadSource.equals('Other'); 


Boolean IsOldLeads = oldLeads.LeadSource == 'Other';
Boolean IsNewLeads = leads.LeadSource == 'Other';

As you have already identified that when LeadSource is null, you are facing the issue. So you just need to invert your check.

    Boolean IsOldLeads = 'Other'.equals(oldLeads.LeadSource);
    Boolean IsNewLeads = 'Other'.equals(leads.LeadSource);

In your case when LeadSource is null it calls null.equals('Other') which is potential candidate for null pointer exceptions.

Thus inverting the null check afterwards makes sure compiler reads the statement as 'Other'.equals(null) which never throws null pointer expcetion.

  • 4
    P.S. This is known as a Yoda condition, coined after the Star Wars character that spoke in OSV (Object-Subject-Verb) form: "Food Nagendra ate" versus the normal SVO "Nagendra ate food". On the one hand, it does avoid NPE (Null Pointer Exceptions), but is sometimes criticized as being more difficult to process cognitively for developers that aren't used to it. I'd probably recommend avoiding random Yoda conditions unless it's consistent elsewhere in the code. This is a perfectly valid solution, though.
    – sfdcfox
    Commented Jan 2, 2023 at 15:15
  • 2
    Always used it I have, the term but never knew. Commented Jan 2, 2023 at 15:20
  • Thanks @sfdcfox Commented Jan 2, 2023 at 15:20
  • Yoda form has the added benefit of avoiding any potential for accidental assignment. I like it because I don't want to spend time debugging typos like "if (i = 0)"
    – Steve Cox
    Commented Jan 2, 2023 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .