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
MonikaalladaMonikaallada 

System.ListException: List index out of bounds: 1

HI...

 

this my testclass.

 

@isTest
private class testloadinginsamepage
{
   static testMethod void Mypage_test()
   {

        Accountdetails  ad = new Accountdetails();

        String Aname ='GenePoint';
        System.assertEquals('GenePoint',Aname); // here success
        List<contact> cn = ad.getcodetails();
        List<Contact> cn1 = [select LastName from contact where Account.name = 'GenePoint' limit 1];
        System.assertEquals('Edna Fra',cn1[0].LastName);
       }

}

 

This is my apex class.

 

public class Accountdetails 
{  
    public String Aname {get; set;}
      public List<contact> getcodetails()
    {
        
          cc =[select name ,Email from Contact where Account.name=:Aname ];
         return cc;
    }

}

 

Can any one please tell me where i am doing the mistake. 

 

kiranmutturukiranmutturu

your are trying to access cn1 [0] but before you need to check if you have the list of records or not

 

 

 List<Contact> cn1 = [select LastName from contact where Account.name = 'GenePoint' limit 1];

 

    if(!cn1.isEmpty())

        System.assertEquals('Edna Fra',cn1[0].LastName);

PrakashbPrakashb

You have not set the aname in your class. It should have been

 

ad.Aname = GenePoint';

 

So i think this assert fails.

 

System.assertEquals('Edna Fra',cn1[0].LastName);

 

 

 

 

MonikaalladaMonikaallada

Hi kiran,

 

here, i did like this

 

List<Contact> cn1 = [select LastName from contact where Account.name ='GenePoint' limit 1];
        if(!cn1.isEmpty()){
        System.assertEquals('Edna Fra',cn1[0].LastName);// here test must fail.
        }

 

Now there is not error but according to assert equals i gave a wrong contact name , so test must fail but it is showing test success.