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
sree sfdcsree sfdc 

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>

Best Answer chosen by sree sfdc
Ramesh KallooriRamesh Kalloori
please go through the below code.

<apex:pageBlockTable value="{!lw}" var="a">
       <apex:column HeaderValue="Account" value="{!a.aname}"/>
       <apex:column HeaderValue="Contact" value="{!a.cname}"/>
       </apex:pageBlockTable>
   </apex:pageBlock>
public List<Wrapper> lw{
  get
  {
  List<Wrapper> Wlist=new List<Wrapper>();
     List<Account> l1=[SELECT Name, (SELECT Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account];
     for(integer i=0;i<l1.size();i++)
     {
         for(integer j=0;j<l1[i].contacts.size();j++)
         {
           wrapper ob=new wrapper(l1[i].name,l1[i].Contacts[j].Lastname);
           Wlist.add(ob);
         }
    }
   return Wlist;
   } 
  set;}
public class wrapper{
  public String aname{get;set;}
  public String Cname {get;set;}
  public wrapper(string aname,string Cname)
  {
      this.aname=aname;
      this.cname=cname;
  }
  }

above code will wors for your requirement.


thanks,
Ramesh

All Answers

Ramesh KallooriRamesh Kalloori
modify the for loop below.

it may not through an error.

FOR(Integer j=0;j<acc.size();j++)
    {
         for(integer i=0;i<con.size();i++)
    {
    lstw.add(new wrapper(acc[j].name,acc[j].accountnumber,con[i].email,con[i].phone));
   }
    }


Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
From your code it seems like you have more contacts than the accounts. Hence you will get list out of bound exception on following line
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
Grazitti TeamGrazitti Team
Hi,


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

Ramesh KallooriRamesh Kalloori
please go through the below code.

<apex:pageBlockTable value="{!lw}" var="a">
       <apex:column HeaderValue="Account" value="{!a.aname}"/>
       <apex:column HeaderValue="Contact" value="{!a.cname}"/>
       </apex:pageBlockTable>
   </apex:pageBlock>
public List<Wrapper> lw{
  get
  {
  List<Wrapper> Wlist=new List<Wrapper>();
     List<Account> l1=[SELECT Name, (SELECT Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account];
     for(integer i=0;i<l1.size();i++)
     {
         for(integer j=0;j<l1[i].contacts.size();j++)
         {
           wrapper ob=new wrapper(l1[i].name,l1[i].Contacts[j].Lastname);
           Wlist.add(ob);
         }
    }
   return Wlist;
   } 
  set;}
public class wrapper{
  public String aname{get;set;}
  public String Cname {get;set;}
  public wrapper(string aname,string Cname)
  {
      this.aname=aname;
      this.cname=cname;
  }
  }

above code will wors for your requirement.


thanks,
Ramesh

This was selected as the best answer