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

List index out of bound error ?
Same code works in anonymous window but in test class it shows list index out of bound error .
@istest
public class setMethod{
public static testmethod void main(){
List<student__c> sts = new List<student__c>();
sts =[select name from student__c ] ;
string temp = sts.get(2).name ;
system.debug(temp);
}
}
@istest
public class setMethod{
public static testmethod void main(){
List<student__c> sts = new List<student__c>();
sts =[select name from student__c ] ;
string temp = sts.get(2).name ;
system.debug(temp);
}
}
Student__c student = new Student__c(Name='test');
insert student;
Student__c student2 = new Student__c(Name='test2');
insert student2;
List<Student__c> sts = [SELECT Name FROM Student__c];
An alternative way to circumnavigate this issue is to mark your testClass with the @isTest(SeeAllData=true) flag, which grants the original level of access to your test class. (This is often not considered best practise though as it makes your test passing dependant on data that may be changed or removed). It can provide a quick fix, or help (short term) test particulally complicated data structures.
Lots more info on this can be found in the developer docs here.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm