Array Index Out of Bounds error on Array List

I’m sure that someone will tell you to format your code, you can do that by highlighting your code and pressing Control + Shift + C. What I did was add to your code:

if(names.size() > 0) 
 {
names.remove(x);
 }
if(names.size() == 0 )
 {
   gameOver();
 }

You’re getting an array index out of bound error, because when you remove the last item in your list, and then you try removing the object at the index of x, there is nothing to be removed. And then I just had an if statement say that if the size is zero, then to show your gameOver() function.

Hopefully this helps? If you have any more questions I would love to help, and please don’t mind keeping us updated on your project. (P.S. Do not forget to format your code next time :smiley: )

EnhancedLoop7

2 Likes