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
Jonathan Osgood 3Jonathan Osgood 3 

What's wrong with my null check? Getting Null pointer exception after update

Hi All,

Getting Null pointer exception on If Statement Only if field is not populated, after update. Added null check to avoid but it didnt work. What is wroing with my Null check?
 
if(l.Website_Inquiry_Type__c.containsIgnoreCase('Buy Generator') 
           ||l.Website_Inquiry_Type__c.containsIgnoreCase('Sell Generator')
            &&  l.Website_Inquiry_Type__c != null  ) 

               { rest of my code that works fine}

 
Best Answer chosen by Jonathan Osgood 3
Dilip_VDilip_V
Jonathan,

First check Website_Inquiry_Type__c  is null or not.
if( l.Website_Inquiry_Type__c != null)
if(l.Website_Inquiry_Type__c.containsIgnoreCase('Buy Generator') 
           ||l.Website_Inquiry_Type__c.containsIgnoreCase('Sell Generator')  ) 

               { rest of my code that works fine}

or Like this
 
if( l.Website_Inquiry_Type__c != null  &&(l.Website_Inquiry_Type__c.containsIgnoreCase('Buy Generator') 
           ||l.Website_Inquiry_Type__c.containsIgnoreCase('Sell Generator'))
           ) 

               { rest of my code that works fine}

Let me know if it works.

Mark it as best answer if it works.

Thanks.

All Answers

Shun KosakaShun Kosaka
Hi,
Operator && is processed prior to ||. Try to put OR part in parentheses.
if((l.Website_Inquiry_Type__c.containsIgnoreCase('Buy Generator') 
           ||l.Website_Inquiry_Type__c.containsIgnoreCase('Sell Generator'))
            &&  l.Website_Inquiry_Type__c != null  ) 

               { rest of my code that works fine}
See also,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_precedence.htm
Dilip_VDilip_V
Jonathan,

First check Website_Inquiry_Type__c  is null or not.
if( l.Website_Inquiry_Type__c != null)
if(l.Website_Inquiry_Type__c.containsIgnoreCase('Buy Generator') 
           ||l.Website_Inquiry_Type__c.containsIgnoreCase('Sell Generator')  ) 

               { rest of my code that works fine}

or Like this
 
if( l.Website_Inquiry_Type__c != null  &&(l.Website_Inquiry_Type__c.containsIgnoreCase('Buy Generator') 
           ||l.Website_Inquiry_Type__c.containsIgnoreCase('Sell Generator'))
           ) 

               { rest of my code that works fine}

Let me know if it works.

Mark it as best answer if it works.

Thanks.
This was selected as the best answer
Jonathan Osgood 3Jonathan Osgood 3
That worked, thanks!