Java – ArrayIndexOutOfBoundsException error while parsing JSON data

ArrayIndexOutOfBoundsException error while parsing JSON data… here is a solution to the problem.

ArrayIndexOutOfBoundsException error while parsing JSON data

I wrote in id[i] = c.getString(TAG_ID); Received ArrayIndexOutOfBoundsException|

The code is as follows:

for (int i = 0; i < 25; i++) {
    JSONObject c = contacts.getJSONObject(i);
    id[i] = c.getString(TAG_ID);
}

I checked that the JSON file contains 25 objects. I’ve also tried using i<10 and it still gives the same error.

Solution

The id should be an array with at least 25 elements to avoid index out-of-bounds.

String [] id = new String[25];

Related Problems and Solutions