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

System.ListException: List index out of bounds: 12
hi developers
pls help me how to reslove this error.
Apex
------------
public class wrapperclass1
{
list<Account> acc= new list<Account>();
list<Contact> con=new list<Contact>();
public List<wrapper> lstw = new List<wrapper>();
public List<wrapper> getListwrapper()
{
acc=[select name,accountnumber from account];
con=[select email,phone from contact];
for(integer i=0;i<con.size();i++)
{
lstw.add(new wrapper(acc[i].name,acc[i].accountnumber,con[i].email,con[i].phone));
}
return lstw ;
}
public class wrapper
{
public String name{get;set;}
public String accountnumber{get;set;}
public String email{get;set;}
public String phone{get;set;}
// Wrapper class constructor
public wrapper(String name,String accountnumber,String email,String phone)
{
this.name=name;
this.accountnumber=accountnumber;
this.email=email;
this.phone=phone;
}
}
}
vf page
------------
<apex:page controller="wrapperclass1">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:pageBlockTable value="{!listwrapper}" var="wap">
<apex:column headerValue="Action">
<apex:inputCheckbox />
</apex:column>
<apex:column headerValue="Account Name">
{!wap.name}
</apex:column>
<apex:column headerValue="Account Number">
{!wap.AccountNumber}
</apex:column>
<apex:column headerValue="Email">
{!wap.Email}
</apex:column>
<apex:column headerValue="Phone">
{!wap.phone}
</apex:column>
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
pls help me how to reslove this error.
Apex
------------
public class wrapperclass1
{
list<Account> acc= new list<Account>();
list<Contact> con=new list<Contact>();
public List<wrapper> lstw = new List<wrapper>();
public List<wrapper> getListwrapper()
{
acc=[select name,accountnumber from account];
con=[select email,phone from contact];
for(integer i=0;i<con.size();i++)
{
lstw.add(new wrapper(acc[i].name,acc[i].accountnumber,con[i].email,con[i].phone));
}
return lstw ;
}
public class wrapper
{
public String name{get;set;}
public String accountnumber{get;set;}
public String email{get;set;}
public String phone{get;set;}
// Wrapper class constructor
public wrapper(String name,String accountnumber,String email,String phone)
{
this.name=name;
this.accountnumber=accountnumber;
this.email=email;
this.phone=phone;
}
}
}
vf page
------------
<apex:page controller="wrapperclass1">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:pageBlockTable value="{!listwrapper}" var="wap">
<apex:column headerValue="Action">
<apex:inputCheckbox />
</apex:column>
<apex:column headerValue="Account Name">
{!wap.name}
</apex:column>
<apex:column headerValue="Account Number">
{!wap.AccountNumber}
</apex:column>
<apex:column headerValue="Email">
{!wap.Email}
</apex:column>
<apex:column headerValue="Phone">
{!wap.phone}
</apex:column>
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
above code will wors for your requirement.
thanks,
Ramesh
All Answers
it may not through an error.
lstw.add(new wrapper(acc[i].name,acc[i].accountnumber,con[i].email,con[i].phone));
I would recommendrelationship query to get account details in contact query itself. This will also increase efficiency of your code as one soql gets eliminated.
con=[select email,phone, account.name, account.accountnumber from contact];
and create wrapper as
lstw.add(new wrapper(con[i].acc.name,con[i].acc.accountnumber,con[i].email,con[i].phone));
If this answers your question, mark thhis as best answer.
-N.J
The error is because of the mismatch of Accounts' and Contacts' count (number of contacts are greater than account that is con.size() is not equal to acc.size() ) that is possibly due to the reason that some contacts might have been created without accounts or more than one contact is associated with same account.
--
Regards,
Grazitti Team
Web: www.grazitti.com
above code will wors for your requirement.
thanks,
Ramesh