function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mamatha Ch 10Mamatha Ch 10 

Apex test class error- List Index out of bounds

Hi,
below is my apex class:
public class CampaignMember_List {
    
    private Id CampaignId{get; set;}
    private final ApexPages.StandardController sc;   
    public CampaignMember_List (ApexPages.StandardController stdController) {    
        this.CampaignId = stdController.getId();
        this.sc = stdController;
    }

    public List<CampaignMember> cms {get;set;}
    
    public String sortField {get; set;}
    public String previousSortField {get; set;}
    
    public List<CampaignMember> getMembers() {
        if(cms == null){
            cms = [SELECT Id, Contact.OptedOutOfShire__c, Phone, Mapping_City_State__c,  Lead.OptedOutOfShire__c  ,ContactId, LeadId, FirstName, LastName, Type__c, 
                   Status, Is_Guest__c, Contact.MailingCity, Contact.MailingState, Contact.Phone, Lead.Phone, Lead.City, Lead.State FROM CampaignMember 
                   WHERE Campaign.Id =: CampaignId];
        }

        return cms;
    }


    public void doSort(){
        String order = 'asc';
        
        /*This checks to see if the same header was click two times in a row, if so 
it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
        
        //To sort the table we need to use this one line
        superSort.sortList(cms,sortField,order);
    }
}
this is the test class:
@istest
    static void testsortmethod()
    {                             
        
        Campaign program = new Campaign(Name='Test');
        insert program;
        
        Lead l1 = new Lead(Firstname='First', LastName='Last',company='nav');
        insert l1;
        
        Lead l2 = new Lead(Firstname='First', LastName='Last',company='van');
        insert l2;
        
        CampaignMember cm1 = new CampaignMember(CampaignId=program.Id, LeadId=l1.Id);
        insert cm1;
        CampaignMember cm2 = new CampaignMember(CampaignId=program.Id, LeadId=l2.Id);
        insert cm2;
        
        List <CampaignMember> members = [SELECT Id, ContactId, LeadId, FirstName, LastName, Type__c, 
                                         Status, Lead.Phone, Lead.City, Lead.State FROM CampaignMember 
                                         WHERE Campaign.Id =: program.Id ] ; 
       
        CampaignMember cm = new CampaignMember();
        ApexPages.StandardController sc = new ApexPages.StandardController(cm);
        CampaignMember_List ms = new CampaignMember_List(sc);
        ms.getMembers();
        system.assert(members.size() > 0);
        
    	  if(members.size() > 0){
            string order = 'asc';
            ms.sortField='FirstName';
        
              ms.doSort();
            superSort.sortList(members,'FirstName','asc');
            
      } 
    }
I am getting 89% coverage but the test class is failing at ms.dosort();
Any ideas??
Thanks for your time

 
sfdcMonkey.comsfdcMonkey.com
hi Mamatha
go to below link for related error Knowledge Article
https://help.salesforce.com/apex/HTViewSolution?id=000181121&language=en_US
.
Thanks I hop it helps you

 
Mamatha Ch 10Mamatha Ch 10
Hi Piyush,
I tried that link before posting my question here. It didn't work. Thanks for your time :)
Siva@51Siva@51
Hi Mamatha,

In test class, at line 24 try this instead of previous.

ApexPages.StandardController sc = new ApexPages.StandardController(program);

Thanks :)
Mamatha Ch 10Mamatha Ch 10
Hi Slva,
Thanks for your reply. I tried your suggestion, now the error is at ms.dosort();: System.NullPointerException: Argument cannot be null.