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
sreeesreee 

System.ListException: List index out of bounds: 5

Iam facing following error System.ListException: List index out of bounds: 5 ...Plz need help..thanks in Advance

 

public with sharing class WrapperIntStringDisplayClassTest {
List<Testing__c> lsttest = new List<Testing__c>();
List<DataLoadTestingClass__c> lstdlt = new List<DataLoadTestingClass__c>();
public List<wrapper> lstw = new List<wrapper>();
public List<wrapper> getLstwrapperIntString() {
lsttest = [select name,city__c from Testing__c];
lstdlt = [select country__c,phone__c from DataLoadTestingClass__c];
for(Integer i=0;i<lstdlt.size();i++){
lstw.add(new wrapper(lsttest[i].name,lsttest[i].city__c,lstdlt[i].country__c,lstdlt[i].phone__c));
}
return lstw;
}


public class wrapper{
public String Tname{get;set;}
public String Tcity{get;set;}
public String Dcountry{get;set;}
public String Dphone{get;set;}

public wrapper(String Tname,String Tcity,String Dcountry,String Dphone){
this.Tname=Tname;
this.Tcity=Tcity;
this.Dcountry=Dcountry;
this.Dphone=Dphone;
}
}
}

 

ReidCReidC

Do you have 6 elements in your list?  Lists are 0 based, so i you have 5 elements, list.get(5) will give you an index out of bounds.

sandeep@Salesforcesandeep@Salesforce

it is happening because 

 

List "lstdlt" is having more then 5 elements 

List "lsttest" is having only 5 elements

 

in order to avoid it you need to apply some check of list size