# java.lang.ArrayIndexOutOfBoundsException

**URL:** <https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529>\
**Category:** Beginners\
**Created:** [December 20, 2023, 9:52pm UTC](https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529 "2023-12-20T21:52:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaluz](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kaluz](https://discourse.processing.org/u/Kaluz)\
**Post date:** [December 20, 2023, 9:52pm UTC](https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529/1 "2023-12-20T21:52:44Z")

</div>

Hello,

i’m new here and a beginner in Java. Also english is not my native language, but i hope u can understand my problem.

I have a problem with my code and can’t solve it myself. I get this error:

“java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 6”

I know what this error means but i can’t find the array that causing this problem. I only can identify in which void the error occurs. But this void has over 3.000 lines of code and several arrays with 6 positions. Also this error does not come every time. I can’t reproduce this error, because i don’t know exactly what went wrong.  
I do a lot of VBA and there the code line in which the error occurs get’s highlighted so you know exactly which array throws the exception. In processing it seems i don’t get the correct line and i don’t see the array.  
I tried the try{} catch (excpetion)} this doesn’t help me.  
How can i identify the array or the code line which throws the exception?  
Is this even possible? The code is pretty messy it’s not good coded and i will recode the whole thing when i’m done with it. But for now, i need to know where the error comes from.

I hope you understand and can help me 🙂

---

<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:** [December 20, 2023, 10:28pm UTC](https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529/2 "2023-12-20T22:28:45Z")

</div>

[why-do-i-get-an-arrayindexoutofboundsexception](https://forum.processing.org/two/discussion/8070/why-do-i-get-an-arrayindexoutofboundsexception.html)

> **[Arrays (The Java™ Tutorials \>        
            Learning the Java...](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [December 21, 2023, 6:32am UTC](https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529/3 "2023-12-21T06:32:39Z")

</div>

Hi @Kaluz,

Without seeing your code no one can determine the error location…

I guess the index is calculated in some ways…

```auto
int[] array = new int[6];
int a=5;
int b=6;
int index=a-b; // -1

int v=array[index]; // throws java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 6

```

**Have you tried to run your sketch in debug mode?**

> **[Environment](https://processing.org/environment/#debug-1)**
>
> The Processing Environment includes a text editor, a compiler, and a display window. It enables the creation of software within a carefully designed set of constraints.

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 21, 2023, 6:57am UTC](https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529/4 "2023-12-21T06:57:37Z")

</div>

In general this error happened if issues in array initialization and use of arrays and indexes

[![](https://img.youtube.com/vi/5tSBbWySCC0/maxresdefault.jpg "Arrays in Processing 3 (Java)") ](https://www.youtube.com/watch?v=5tSBbWySCC0)

> **[Array / Reference](https://processing.org/reference/array)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

> <https://stackoverflow.com/questions/72120229/how-do-i-debug-arrays>

---

<div class="post-metadata">

**Author:** ![JiyaHana](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jiyahana/32/19209_2.png) [@JiyaHana](https://discourse.processing.org/u/JiyaHana)\
**Post date:** [December 22, 2023, 7:31am UTC](https://discourse.processing.org/t/java-lang-arrayindexoutofboundsexception/43529/5 "2023-12-22T07:31:48Z")

</div>

If possible, then post you code here it might be possible someone solve you issue and you can also check [java.lang.ArrayIndexOutOfBoundsException](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ArrayIndexOutOfBoundsException.html).
