+ Start a Discussion
Pervaz AllaudinPervaz Allaudin 

System.ListException: List index out of bounds: 0

I am doing thhe Trailhead excerize of process automation annd I get the 
System.ListException: List index out of bounds: 0
It does not say how annd where.

I redid the the one picklist - Type. and the Field update functions and all the places where the pcklist values are referenced.
But I am still getting the error.

I have not learned the debug mode and not in the visual force language yet.

Could someone please direct me to solve the issue.

Thanks in advance.
Pevaz 
UC InnovationUC Innovation
This error occurs when you're trying to access an array's 5th element for example but the array only holds 4 elements. It would be helpful if you could post your code, but I'd recommend just looking to see how you're accessing your arrays.
FearNoneFearNone
hi pervaz,

it means your list is null or empty.
and when it try to read list[0], error.
Nishad KNishad K
Hi Pervaz,

This means that your query has returned no records that match the criteria.  If you then attempt to access an element at row 0, this will throw an error as that row doesn't exist.

If you then attempt to access an element at row 0, it'll throw an error as that row doesn't exist. It's good practice to check there are records first in order to make sure that the list is not empty before accessing it.
see:
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 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!!

Considerations for empty list
  • The query returned no rows to the List after execution.
  • You are also using those field(s) in your class or trigger.
  • In this case you will get a System.ListException: List index out of bounds: 0

Regards