Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Standard ListViews Adding a Gradient

Stephen Little
Stephen Little
8,312 Points

JSONException error about index 49?

it was working fine so I'm not sure where I went wrong, I may have to redo all the video's to find it. This is the error I'm getting when I run in debugging.

E/MainActivity﹕ Exception caught: org.json.JSONException: Index 49 out of range [0..49)

Any help would be awesome at this point, I'm guessing there is something going wrong with the length() or something. Will keep pounding my head on it for a few more hours but for now taking a much needed coffee break :)

cheers! Stephen

Vrezh Gulyan
Vrezh Gulyan
11,161 Points

There should only be 48 items in the array. I need to see the code for your for loop but I am going to guess that it looks something like i <= data.length() when it should be i < data.length(). If you put less then or equal to you would get an index out of bounds exception because you are trying to access array[49] when there is no such element.

2 Answers

Hello,

I'm going to guess you have your loop going in the form

for (int i = 0; i <= arrayList.length(); i++)

if so, change the <= to just a < since arrayList.length() returns the total number of elements in the ArrayList, however indexing starts at 0 so it goes from 0 to length() - 1.

Stephen Little
Stephen Little
8,312 Points

Yip that was what I was thinking as well, I went through the code and found the instead of "i" I put a 1. Not really sure how got that way seeing how "1" is nowhere near "i" lol but thanks all, awesome people here!

Stephen

Hiya Stephen,

This is a rare one in these forums ... an exception that has been caught by the code and is exiting, almost, gracefully! The error should let you know what line that occurred at ... can you share that line of code?

However, this is an array overflowing so that a loop is exceeding the number of elements of things it is looping through.

I hope that makes sense!

Steve.