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

1 Answer 1

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. Commented Dec 2, 2016 at 8:56
  • @SFDC_Learner - pretty easy to add that. See updated answer
    – Eric
    Commented Dec 2, 2016 at 8:58

You must log in to answer this question.

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