0

I have below code where I get error mentioned in the subject when I try to add value to a set of String

 public Set<String> Recordtypes {get;set;}

Please find the line in code which throws error.

Code Snippet:

 for(Compliance_Incident_abv__c inc:(List<Compliance_Incident_abv__c>)contr.getSelected())
    {
        if(Ids == '')
           { Ids=inc.Id;
             Ids1=inc.Id;
             if(inc.recordtypeid!=null)
             recordtypes.add(inc.Recordtypeid); // Error throws
           }
        else
            {Ids = Ids + ';' +inc.Id;
              Ids1 = Ids1 + ',' +inc.Id;
            if(inc.recordtype.developername!=null)
            recordtypes.add(inc.Recordtypeid); // Error throws
            }

 if((inc.Status_abv__c=='New' || inc.status_abv__c == 'Under Review') && counter==0 )
            {Counter=Counter+1;}
             system.debug('Counter@'+counter);
              system.debug('Status@'+inc.Status_abv__c);

         if(((inc.Status_abv__c!='New') && (inc.status_abv__c != 'Under Review')) && change1==false ) //
        {
         //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Your selected Compliance Incident : '+inc.Name+' is not having Status as \'New\'!'));
        pagemsg=true;chkpub2=true;
        Errorstr = Errorstr+','+inc.Name;// CONCATE
        system.debug('Errorstr@'+Errorstr);
        }           
}

1 Answer 1

2

Simply add a condition like:

if(recordtypes == null)
{
    recordtypes = new Set<Id>(); // or new Set<String>();
}

recordtypes.add(inc.Recordtypeid);

Also in property define set as:

Set<id> recordtypes { get 
                         { 
                             if(recordtypes==null)
                             {
                                   recordtypes = new Set<Id>();
                             }
                             return recordtypes;
                         } set;
                     }

You must log in to answer this question.

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