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 trialStephen Little
8,312 PointsJSONException 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
2 Answers
James Simshaw
28,738 PointsHello,
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
8,312 PointsYip 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
Steve Hunter
57,712 PointsHiya 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.
Vrezh Gulyan
11,161 PointsVrezh Gulyan
11,161 PointsThere 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.