Fix possible crash when the index is greater than the number of element