1

Can someone please help me to convert String array values to Set of string? I have below code where it says Set is not getting populated from string array.

 Criteria=[SELECT Franchise__c FROM Sample_Order_Campaign__c WHERE Name =:selCampaignName];
          for(i=0;i<Criteria.size();i++)
          {if(i>0)
          Splitarray= Splitarray+';;'+Criteria[i].Franchises__c;
           else {Splitarray=Criteria[i].Franchises__c;}   
          }
          Splitarray=Splitarray.replaceAll(';;', ',');
          system.debug('Splitarray@@'+Splitarray);
          string[] splits= Splitarray.split(',');
          system.debug('splits@@'+splits);
          Splitlist.addall(splits); // Attempt to dereference null object error throws
          system.debug('splitlist@@'+splitlist); 
0
3

From what I could tell is you want to end up with a unique list of Franchise__c strings from the queried records, if so....

You code could be reduced to this:

Set<String> SplitList = New Set<String>();

for(Sample_Order_Campaign__c so : [SELECT Franchise__c FROM Sample_Order_Campaign__c WHERE Name =:selCampaignName]){
    IF(!string.isBlank(so.Franchise__c)) SplitList.addAll(so.Franchise__c.split(';;'));    
}

system.debug(SplitList);

The reason why you were getting a DNO was because you never initialized you SplitList

2
  • But I my Franschise__c value is like 'A;;B;;C'. I need to split it and then put into the Set. – Testing_SFDC Dec 2 '16 at 8:56
  • @SFDC_Learner - pretty easy to add that. See updated answer – Eric Dec 2 '16 at 8:58

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.