0

I am getting the above error at below line. Can someone Please let me know? It should be true or false , why null?

Apex class

   List<Account>  Accnt= [select ID , Name , Restricted_Products_vod__c from Account where ID=:Test.Account_abv__c];
if(!Accnt.isEmpty()){
    Restrict = Accnt[0].Restricted_Products_vod__c ;
}
         List<Product_vod__c>  prd= [select ID , Name  from Product_vod__c where ID=:ProductID];
if(!prd.isEmpty()){
    Prodrestrict= prd[0].Name;
}
    if(Restrict.contains(Prodrestrict)) //This line throws error
         {
           ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Selected Product is restricted for selected Account.');
                    ApexPages.addMessage(Msg);
         }
  • What is Prodrestrict ? A variable ? What value does it contain ? – Mihai Neagoe Feb 19 '16 at 17:03
  • I'm guessing that Restrict is null. That's the only way you'd get that error. Try putting System.assertNotEquals(null, Restrict); on the line before to see if it fails. – sfdcfox Feb 19 '16 at 17:03
  • I have edited with both are public strings with getter setters – Testing_SFDC Feb 19 '16 at 17:07
  • Restricted_Products_vod__c may be null on Account – kurunve Feb 19 '16 at 17:20
  • Add last if condition inside above if like this if(!prd.isEmpty()){ Prodrestrict= prd[0].Name; if(Restrict.contains(Prodrestrict)) //This line throws error { ApexPages.Message Msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Selected Product is restricted for selected Account.'); ApexPages.addMessage(Msg); } } – Ratan Paul Feb 19 '16 at 17:30
1

You need to do a null check:

if (String.isNotBlank(Restrict) && Restrict.contains(ProdRestrict))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.