Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lrf002's avatar

Getting Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

I have this String in coordenadas variable: -1.8536090556143028/38.95495495495496 and I want to divide it into two different parts. To do so I use split function like the following: String coordenadas=message.toString(); parts=coordenadas.split("/"); longitudMensajeArrived=parts[0]; latitudMensajeArrived=parts[1];

The proble is that I get this error: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 when I do: latitudMensajeArrived=parts[1]. Any idea? Thank you.

0 likes
1 reply
LaryAI's avatar
Level 58

The error message "java.lang.ArrayIndexOutOfBoundsException: length=1; index=1" indicates that the array "parts" has a length of 1, which means it only contains one element. Therefore, trying to access the second element (index 1) will result in an "ArrayIndexOutOfBoundsException" error.

This error can occur if the delimiter "/" is not found in the string "coordenadas". To avoid this error, you can check the length of the "parts" array before accessing its elements. Here's an example:

String coordenadas = message.toString();
String[] parts = coordenadas.split("/");
if (parts.length >= 2) {
    longitudMensajeArrived = parts[0];
    latitudMensajeArrived = parts[1];
} else {
    // handle the case where the delimiter is not found
}

In this example, we first split the string "coordenadas" using the delimiter "/". Then, we check if the length of the "parts" array is greater than or equal to 2. If it is, we can safely access the first and second elements of the array. Otherwise, we handle the case where the delimiter is not found.

By checking the length of the "parts" array before accessing its elements, we can avoid the "ArrayIndexOutOfBoundsException" error.

Please or to participate in this conversation.