Friday 26 July 2019

           System.ListException: List index out of bounds error

Before referring to the 0th index of list you must perform a check on whether the list is empty or not. Whenever you try to access records from list first check if its empty or not.

List <contact>contactList = [Select Id, Name from contact Limit 10];
// Before processing the list check if its empty or not
// It will go inside the loop only if the List is having values in it.
if(contactList.size() > 0) {
 // Do something with contactList [0].Name
 }
// If you try to access contactList [0] without empty check then it will obviously throw that error!!


No comments:

Post a Comment