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);
         }
6
  • What is Prodrestrict ? A variable ? What value does it contain ? Feb 19, 2016 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, 2016 at 17:03
  • I have edited with both are public strings with getter setters Feb 19, 2016 at 17:07
  • Restricted_Products_vod__c may be null on Account
    – kurunve
    Feb 19, 2016 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, 2016 at 17:30

1 Answer 1

1

You need to do a null check:

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

You must log in to answer this question.

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