0

Here is the code:

for(Requests curRequest : requestList){
       if (curRequest.varSRProduct!= null ){
            for(SObject so : curRequest.varSRProduct){
             //code
            }
       }
}

Wrapper Class: Wrapper class

global class Requests {
     @InvocableVariable global Boolean isActive;
     @InvocableVariable global Boolean srBoolean;
     @InvocableVariable global List<SObject> varSRProduct = new List<SObject>();
}
global class Results {
    @InvocableVariable global List<SObject> varSupportedProducts;
}
  • requestList - Wrapper class
  • varSRProduct - List of objects

Tried isEmpty, list size(). Nothing is working except requestlist[0].varSRProduct!= null which is not a good practice.

3
  • 1
    Can you please share your wrapper class as well.
    – Mahmood
    Commented Sep 21, 2021 at 10:55
  • Hi @Adarsh - can you please let us know how are you setting the varSRProduct in your code.
    – Mahmood
    Commented Sep 21, 2021 at 12:03
  • we are having this error when varSRProduct is null which is coming from the Lightning Flow. But when I do null check I get this exception Commented Sep 21, 2021 at 12:31

2 Answers 2

1

There seems to be a problem with you wrapper class object instantiation. If the varSRProduct is mandatory component of the wrapper then it has to be initialized properly with initialization of wrapper. To elaborate my point, please understand below points

e.g. Following example will always produce null

List<sobject> sobjectlist;
System.debug(sobjectlist);  // results in <null>

To correct the above issue, you could check if it is null or not

if(sobjectlist != null) { 
   // then add items to the list
   // perform another <if> check to perform an data integrity on the list items
}

OR, you should initialize it correctly

List<sobject> sobjectlist = new List<sobject>(); 
system.debug(sobjectlist);   // results in <()> - an empty list
OR
List<sobject> sobjectlist = [SELECT Id FROM sobject];
system.debug(sobjectlist);   // results in <{item1, itemsx.. }> - list of sobject

Now, I know the list itself can't be null, so I will just check for the items using either isEmpty() or size() methods. So, may be you need to recheck your wrapper class code and see how you are initializing the varSRProduct list.

2
  • List<sobject> sobjectlist = new List<sobject>(); - This is how I initialized. But still faced the same issue! Also the if condition if(requestList !=null){ if(curRequest.varSRProduct!= null ) }} Wrapper class Commented Sep 21, 2021 at 10:58
  • Can you please edit your question and add your wrapper class in your question.
    – Mahmood
    Commented Sep 21, 2021 at 10:59
0

Looks like in your code, the curRequest.varSRProduct is null. Therefore, if you will call on null the isEmpty() method, you will get null pointer exception.

Using curRequest.varSRProduct != null && !curRequest.varSRProduct.isEmpty() should solve your problem with exception.

Nevertheless, the !curRequest.varSRProduct.isEmpty() part is probably not needed as if the list is empty the iteration will be skipped.

3
  • curRequest.varSRProduct != null - I even tried that but same exception!:( Commented Sep 21, 2021 at 10:38
  • then the error is happening somewhere else in your code, pls update your question with rest of the code
    – Damecek
    Commented Sep 21, 2021 at 10:41
  • This is the line where the error occurs. If I replace the if condition with --> if(requestlist[0].varSRProduct!= null) there is no issue. Commented Sep 21, 2021 at 10:46

You must log in to answer this question.

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