# Array.length error

**URL:** <https://discourse.processing.org/t/array-length-error/12217>\
**Category:** Beginners\
**Created:** [June 20, 2019, 8:21pm UTC](https://discourse.processing.org/t/array-length-error/12217 "2019-06-20T20:21:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [June 20, 2019, 8:21pm UTC](https://discourse.processing.org/t/array-length-error/12217/1 "2019-06-20T20:21:25Z")

</div>

Whenever I do the array.length function I get the error "The global variable “length” does not exist.

```auto
for (int i = 0; i < walls.length; i++) {
    Wall wall = walls.get(i);
    wall.show();
  }

```

walls is an array of the Wall class

---

<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:** [June 20, 2019, 8:32pm UTC](https://discourse.processing.org/t/array-length-error/12217/2 "2019-06-20T20:32:59Z")

</div>

> [@FlyingApple](#):
>
> Whenever I do the `array.length` function…

_length_ isn’t a function but a field (class property).

> [@FlyingApple](#):
>
> _walls_ is an array of the Wall class.

If it is so, why are you invoking method **get()** over _walls_? `walls.get(i);`

Java arrays only have methods inherited from the Object class, and nothing more:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html)

Probably your _walls_ is an ArrayList instead:

> **[ArrayList / Reference](https://processing.org/reference/ArrayList.html)**
>
> An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is…

If so, invoke its method **size()** instead of an array’s field _length_:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#size()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#size())

Or even better, go w/ an “enhanced” `for ( : ) {}` loop:

> **[for / Reference](https://processing.org/reference/for.html)**
>
> Controls a sequence of repetitions. A basic for structure has three parts: init, test, and update. Each part must be separated by a semicolon (;). The loop continues until …

---

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [June 21, 2019, 11:59am UTC](https://discourse.processing.org/t/array-length-error/12217/3 "2019-06-21T11:59:39Z")

</div>

Thanks a bunch. Great person. Big brain.

---

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [June 21, 2019, 12:00pm UTC](https://discourse.processing.org/t/array-length-error/12217/4 "2019-06-21T12:00:34Z")

</div>

You were correct

> Probably your _walls_ is an ArrayList instead

---

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [June 21, 2019, 12:09pm UTC](https://discourse.processing.org/t/array-length-error/12217/5 "2019-06-21T12:09:38Z")

</div>

Ignore this message i did a mistake

---

<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:** [June 21, 2019, 12:26pm UTC](https://discourse.processing.org/t/array-length-error/12217/6 "2019-06-21T12:26:32Z")

</div>

Yup! But your posted example doesn’t do that… 😌

> [@Help with a short game code](https://discourse.processing.org/t/help-with-a-short-game-code/11108/2):
>
> We can’t use method remove() when using an “enhanced” for ( : ) {} style! no_entry_sign Use a vanilla for ( ; ; ) {} style instead. Preferably, w/ backwards iteration: back

---

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [June 21, 2019, 12:27pm UTC](https://discourse.processing.org/t/array-length-error/12217/7 "2019-06-21T12:27:45Z")

</div>

Yeah i realised just after doing it xD. Thanks tho. Great help.
