# Create and objects in ArrayList when mouse is pressed

**URL:** https://discourse.processing.org/t/create-and-objects-in-arraylist-when-mouse-is-pressed/34887
**Category:** Beginners
**Created:** [January 29, 2022, 5:54pm UTC](https://discourse.processing.org/t/create-and-objects-in-arraylist-when-mouse-is-pressed/34887 "2022-01-29T17:54:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![alex119](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alex119](https://discourse.processing.org/u/alex119)
#### Post date: [January 29, 2022, 5:54pm UTC](https://discourse.processing.org/t/create-and-objects-in-arraylist-when-mouse-is-pressed/34887/1 "2022-01-29T17:54:53Z")

</div>

Hello,

I’m following natureofcode book and I’m trying to make an exercise where some objects are shot out of a cannon.  
The main issue I’m facing is that when I verify for mousePressed, I add to an ArrayList a new bullet object.  
After this, I want to use the object from the ArrayList but I get IndexOutOfBoundsException. So here is my code:

```auto
ArrayList<Bullet> bulletList = new ArrayList<Bullet>();

void setup()
{
    size(1200, 800);
}

void draw()
{
 if(mousePressed)
    {
       bulletList.add(new Bullet());
      
    }
    println(bulletList.get(0).location.x);
}

```

Why do I get this ?

Thanks in advance !

---

<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: [January 29, 2022, 6:08pm UTC](https://discourse.processing.org/t/create-and-objects-in-arraylist-when-mouse-is-pressed/34887/2 "2022-01-29T18:08:52Z")

</div>

Please remember

**Initially there is no 0th element because the list is empty**

* * *

**Try**

```auto
if(bulletList.size()>0)
     println(bulletList.get(0).location.x);

```

**OR**

Or move println inside the if clause {…}

```auto
 if(mousePressed)
    {
       bulletList.add(new Bullet());
       println(bulletList.get(0).location.x);      
    }

```

---

<div class="post-metadata">

### Author: ![alex119](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alex119](https://discourse.processing.org/u/alex119)
#### Post date: [January 29, 2022, 6:17pm UTC](https://discourse.processing.org/t/create-and-objects-in-arraylist-when-mouse-is-pressed/34887/3 "2022-01-29T18:17:26Z")

</div>

Thanks for your quick reply. Apparently it’s working, but I still don’t understand, why do I have to check if the size is greater than 0, to use the object inside the ArrayList !?

---

<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: [January 29, 2022, 6:46pm UTC](https://discourse.processing.org/t/create-and-objects-in-arraylist-when-mouse-is-pressed/34887/4 "2022-01-29T18:46:19Z")

</div>

> [@alex119](#):
>
> why do I have to check if the size is greater than 0, to use the object inside the ArrayList

When the list is empty, its size() is = 0.

it’s empty. Nada. Nothing. Niente.

So when you say `bulletList.get(0)` you try to access an element that isn’t there (number 0).

This gives you an error.

(apparently it works when you pressed the mouse because then the ArrayList has an entry or a few entries)

**TRY**

```auto
println(bulletList.size()); 

```
