+ Start a Discussion

Please help to solve List index out of bound :0 problem



Please help to resolve the problem if recProfileId is getting null then its showing  List index out of bound :0 problem


How can resolve this problem in below code:


public static Integer getRoundingNumber(String recProfileId)

List<Supplier_record__C> sp = [select Round_Final_recordt_to__c from Supplier_record__C where id =: recProfileId];
if(sp[0].Round_Final_recordt_to__c!= null && sp.size()>0)
Integer round = Integer.valueof(sp[0].Round_Final_recordt_to__c);
system.debug('-----------Round169-----------'+round );
return round ;
return 4;





Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Change if condition to..


If(sp.size()>0 && sp[0].Round_Final_Recordt_to__c!=null)


In your code first tou are evaluating sp[0].Round.....__c --> here it will throw error if list size is 0


So you in this case you will have to check for list size first to avoid that error.