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
FinneyFinney 

System.ListException: List index out of bounds: 1

Hi,

I have the follwoing test class but its giving me an error when running a test.

Need your help on fixing it please

The error is 

Class.Test_SelectLineitemController.test_method_1: line 55, column 1


@istest
public class Test_SelectLineitemController {
    @testsetup static void setupData(){
        
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
        
        insert Test_cls_DCC_DFPDatafactory.getTestGoogleParametersList();
        
        Account acc = new account(name='testaccname');
        insert acc;
        List<Opportunity> lstopp = new List<Opportunity>();
        lstopp.add(Test_cls_DCC_DFPDatafactory.getTestOpportunity('Booking',100));
        lstopp.add(Test_cls_DCC_DFPDatafactory.getTestOpportunity('Proposal',30));
        lstopp[0].Is_Approved__c = true;
        lstopp[0].accountid = acc.id;
        insert lstopp;
        
         List<OpportunityLineItem> lstItems = new List<OpportunityLineItem>();
        lstItems.add(Test_cls_DCC_DFPDatafactory.getTestOpportunityLineItem(lstopp[0].id,'Test DFP Product',1));
        for(OpportunityLineItem Oli: lstItems){ //loop to the line items
        Oli.DFP_Id__c = string.valueof(system.now()).replaceAll('-', '').replaceAll(':', '').replaceAll(' ', '');
        Oli.DFP_External_Id__c = Oli.DFP_Id__c;
        Oli.Description = 'Test';
        Oli.Start_Date__c = System.Today();
        Oli.End_Date__c = System.Today() + 5;
        insert lstItems;
     }   
        insert Test_cls_DCC_DFPDatafactory.getTestDFPMapping();
        
    }
    
    
    static testMethod void test_method_1(){
        test.startTest();
        {
            SelectLineItemsController obj = null;
            PageReference pageRef = Page.SelectLineItems;
            Test.setCurrentPage(pageRef);
            obj = new SelectLineItemsController();
            pagereference cancel1 = obj.cancel();
            pagereference Sync_DFP1 = obj.Sync_DFP();
            Opportunity opp = [select id,name,(select id,name from opportunitylineitems limit 2) from opportunity where Is_Approved__c = true limit 1 ];
            ApexPages.currentPage().getparameters().put('id',opp.id);
            obj = new SelectLineItemsController();
            cancel1 = obj.cancel();
            
            Sync_DFP1 = obj.Sync_DFP();

            obj.lstwrapper[0].isselected = true;
            
            obj.lstwrapper[1].isselected = true;
            Sync_DFP1 = obj.Sync_DFP();
            
            obj.oppRecord.stagename='Proposal';
            obj.oppRecord.Trafficker_DFP_Id__c = '';
            obj.oppRecord.Targeting_Location__c = '';
            obj.oppRecord.Advertiser_DFP_Id__c = '';
            
            obj.oppRecord.opportunitylineitems[0].Description__c = '';
            obj.oppRecord.opportunitylineitems[0].Start_Date__c = null;
            obj.oppRecord.opportunitylineitems[0].End_Date__c = null;
            obj.oppRecord.opportunitylineitems[0].Product2.Height__c = '';
            obj.oppRecord.opportunitylineitems[0].Product2.Size__c = '';
            obj.oppRecord.opportunitylineitems[0].Description__c = 'Test';
            obj.oppRecord.opportunitylineitems[1].Start_Date__c = system.today().adddays(-2);
            obj.oppRecord.opportunitylineitems[1].End_Date__c = system.today().adddays(-3);
            Sync_DFP1 = obj.Sync_DFP();
            
        }
        test.stopTest();
    }
    
    static testMethod void test_method_2(){
         test.startTest();
        {
            string str = SelectLineItemsController.ValidateOpp(null);
        }
        test.stopTest();
    }
    
}
NagendraNagendra (Salesforce Developers) 
Hi Finney,

Before referring to the 0th index of a list you must perform a check on whether the list is empty or not. Whenever you try to access records from list first check if it's empty or not like it is done below: List lstAccount = new List(); // Fill the list... // Before processing the list check if it's empty or not // It will go inside the loop only if the List is having values in it. if(lstAccount.size() > 0){ // Do something with lstAccount[0].Name }.

Always before accessing the list, you must check whether the list is empty.
 
An example here would make more clear:
List lstAccount = [Select Id, Name from Account Limit 10];

// Before processing the list check if its empty or not
// It will go inside the loop only if the List is having values in it.
if(lstAccount.size() > 0) {
    // Do something with lstAccount[0].Name
}

// If you try to access lstAccount[0] without empty check then it will obviously throw that error!!
Please let us know if this helps.

Mark this as solved if it's resolved.

Thanks,
Nagendra

 
FinneyFinney
Hi Nagendra,

This is the line that is causing the error.

           Sync_DFP1 = obj.Sync_DFP();

            obj.lstwrapper[0].isselected = true;
            
            obj.lstwrapper[1].isselected = true;
            Sync_DFP1 = obj.Sync_DFP();