This article explains why a 'System.ListException: List index out of bounds: 0' error occurs when trying to access a list, and how to resolve it.
If we attempt to access an element at row 0, the code will throw an error if no data exists at row 0. It is a good practice to check whether there are records in the list before accessing any element from that list.
Before referring to the 0th index of a list, here is sample code to check on whether the list is empty or not.
List lstAccount = [Select Id, Name from Account 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(lstAccount.size() > 0) {
// Do something with lstAccount[0].Name
}
// If you try to access lstAccount[0] without empty check then it will obviously throw that error!!
000385743

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.