You need to sign in to do that
Don't have an account?

System.ListException: List index out of bounds: 6 Class.tt.<init>: line 14, column 1
The code is giving me an error.
System.ListException: List index out of bounds: 6 Class.tt.<init>: line 14, column 1
Please help me on this.
APEX CONTROLLER
----------------------------------------------------------------
public class tt
{
public List<Account> accLst {get; set;}
public List<Contact> conLst {get; set;}
public List<MyWrapper> wrapper {get; set;}
public tt()
{
accLst = [select id,name from account ] ;
conLst = [select id,name from contact ] ;
wrapper = new List<MyWrapper>() ;
for(Integer i=0 ;i<20;i++)
wrapper.add(new MyWrapper(accLst[i] , conLst[i])) ;
}
public class MyWrapper
{
public Account accRec {get; set;}
public Contact conRec {get; set;}
public MyWrapper(Account acc , Contact con)
{
accRec = acc ;
conRec = con ;
}
}
}
VisualForce Page
<apex:page controller="tt">
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!wrapper}" var="wrap">
<apex:column headerValue="Account Name" value="{!wrap.accRec.Name}"/>
<!-- you can add related fields here -->
<apex:column headerValue="Contact Name" value="{!wrap.conRec.Name}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
System.ListException: List index out of bounds: 6 Class.tt.<init>: line 14, column 1
Please help me on this.
APEX CONTROLLER
----------------------------------------------------------------
public class tt
{
public List<Account> accLst {get; set;}
public List<Contact> conLst {get; set;}
public List<MyWrapper> wrapper {get; set;}
public tt()
{
accLst = [select id,name from account ] ;
conLst = [select id,name from contact ] ;
wrapper = new List<MyWrapper>() ;
for(Integer i=0 ;i<20;i++)
wrapper.add(new MyWrapper(accLst[i] , conLst[i])) ;
}
public class MyWrapper
{
public Account accRec {get; set;}
public Contact conRec {get; set;}
public MyWrapper(Account acc , Contact con)
{
accRec = acc ;
conRec = con ;
}
}
}
VisualForce Page
<apex:page controller="tt">
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!wrapper}" var="wrap">
<apex:column headerValue="Account Name" value="{!wrap.accRec.Name}"/>
<!-- you can add related fields here -->
<apex:column headerValue="Contact Name" value="{!wrap.conRec.Name}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
List outof bound exception id generally throw when we try to access the index in a list which is not there ,means you are interating the loop 20 times ,however your account and contact list may not contain those many records .
I think you will catch your issue now .