# Calling a PShape too many times

**URL:** <https://discourse.processing.org/t/calling-a-pshape-too-many-times/33209>\
**Category:** Coding Questions\
**Created:** [November 1, 2021, 2:43am UTC](https://discourse.processing.org/t/calling-a-pshape-too-many-times/33209 "2021-11-01T02:43:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [November 1, 2021, 2:43am UTC](https://discourse.processing.org/t/calling-a-pshape-too-many-times/33209/1 "2021-11-01T02:43:47Z")

</div>

Hi there -

I am calling PShape Shape1 within a for loop.

```auto
for (Shape1 mod : mods) {
       mod.display();

```

I have set a value for an array of mods elsewhere in the code.

```auto
  mods = new Shape1[count*someother];

```

My issue is that I seem to have to use every single “mod” in “mods”, or I will get a NullPointerException. I am currently populating the array something like this:

```auto
 for (int z = 0; z < 20; z++){
      mods[index++] = new Shape1(10-z, 20+z, 30*z); 
              }

```

I’d like to vary the number of these shapes (the “20”) that I display on a any given instance…something like:

`for (int z = 0; z < int(random(20)); z++)`

Can anyone suggest an approach? Thank you. I can share more of the code, if necessarily - but I didn’t want to confuse the question.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 1, 2021, 5:58pm UTC](https://discourse.processing.org/t/calling-a-pshape-too-many-times/33209/2 "2021-11-01T17:58:57Z")

</div>

Hi @bcabc,

> [@bcabc](#):
>
> `mods = new Shape1[count*someother];`

To store your shapes, you are using the Array class. As stated in the [Oracle documentation](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html):

> An _array_ is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, **its length is fixed**.

Most of the time, the issue with fixed length arrays is that in your program you don’t know how much elements you are going to store before creating the array. That’s why you need **variable-sized arrays**.

For this you can use the [`ArrayList`](https://processing.org/reference/ArrayList.html) class:

```processing
// Declare a new dynamic array, it's empty by default
ArrayList<Shape1> mods = new ArrayList<Shape1>();

// You can then randomly add elements as much as you want
// It's going to dynamically grow for you
for (int z = 0; z < int(random(20)); z++) {
  mods.add(new Shape1(...));
}

// You can loop through elements just like an Array
for (Shape1 mod : mods) {
  // So something with mod
}

```

It’s super useful and you [shouldn’t worry about performance](https://stackoverflow.com/questions/19389609/array-vs-arraylist-in-performance/19390034).
