# Index out of bounds

**URL:** <https://discourse.processing.org/t/index-out-of-bounds/25775>\
**Category:** Beginners\
**Created:** [November 27, 2020, 8:25pm UTC](https://discourse.processing.org/t/index-out-of-bounds/25775 "2020-11-27T20:25:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScriptKiddie](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@ScriptKiddie](https://discourse.processing.org/u/ScriptKiddie)\
**Post date:** [November 27, 2020, 8:25pm UTC](https://discourse.processing.org/t/index-out-of-bounds/25775/1 "2020-11-27T20:25:41Z")

</div>

Hi everyone,  
I’m hoping I’m missing something simple here. I want to display an object (which works) and when my player entity collides with this object the object should disappear. My collision code works but once the collision occurs my game crashes giving me this error:  
IndexOutOfBoundsException: Index: 0, Size: 0  
As far as I can tell there is an element in my array list at index 0. Can anyone please point out what I’m missing?

thanks!

```auto
ArrayList<Test> testList = new ArrayList(); //creates arrayList

void setup()
{
	testList.add(new Test((0, 0, 60)); //adds element to arrayList
}

void draw()
{
	testList.get(0).showList(); 
  	if (testList.get(0).testHit()) //this has been set to return true
  	{
  		testList.clear(); //clears arrayList and screen
  	}
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 27, 2020, 11:13pm UTC](https://discourse.processing.org/t/index-out-of-bounds/25775/2 "2020-11-27T23:13:28Z")

</div>

After clearing the list is empty

Therefore you cannot access get(0) because 0 would be the first element but it does not exist

Size is 0

You can avoid the error by using if (…size() \> 0) before the line

Or by setting a variable youLost to true

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 28, 2020, 7:40am UTC](https://discourse.processing.org/t/index-out-of-bounds/25775/3 "2020-11-28T07:40:25Z")

</div>

> [@Chrisir](#):
>
> You can avoid the error by using `if (…size() > 0)` before the line

Alternatively call method **isEmpty()** and use the unary logical operator `!`:  
[https://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#isEmpty()](https://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#isEmpty())

> **[! (logical NOT) / Reference](https://processing.org/reference/logicalNOT.html)**
>
> Inverts the Boolean value of an expression. Returns true if the expression is false and returns false if the expression is true. If the expression (a\>b) evaluates to…

```auto
if (!testList.isEmpty()) {
  final Test t = testList.get(0);
  t.showList();
  if (t.testHit()) t.clear();
}

```

---

<div class="post-metadata">

**Author:** ![ScriptKiddie](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@ScriptKiddie](https://discourse.processing.org/u/ScriptKiddie)\
**Post date:** [November 28, 2020, 9:25am UTC](https://discourse.processing.org/t/index-out-of-bounds/25775/4 "2020-11-28T09:25:48Z")

</div>

Thank you! This has resolved my issue and I realise my mistake. I can finally move on 😄

---

<div class="post-metadata">

**Author:** ![ScriptKiddie](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@ScriptKiddie](https://discourse.processing.org/u/ScriptKiddie)\
**Post date:** [November 28, 2020, 9:27am UTC](https://discourse.processing.org/t/index-out-of-bounds/25775/5 "2020-11-28T09:27:30Z")

</div>

Thanks for the response Chrisir, this has worked wonders and I can now progress on my project
