java.lang.ArrayIndexOutOfBoundsException

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 :slight_smile:

why-do-i-get-an-arrayindexoutofboundsexception

2 Likes

Hi @Kaluz,

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

I guess the index is calculated in some ways…

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?

Cheers
— mnse

2 Likes

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

1 Like

If possible, then post you code here it might be possible someone solve you issue and you can also check java.lang.ArrayIndexOutOfBoundsException.