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 trialDavid Eichel
28,860 PointsArrayIndexOutOfBoundsException??? I saw the exception twice in the video with no mention of it.
I'm wondering why there was an exception. I was looking through the code and stepped through the debugger, but I couldn't figure out why there was an out of bounds exception. As far as I could tell the index with within the array bounds.
Yeeka Yau
7,410 PointsI noticed this in the video too. I'm assuming this might get dealt with later? I agree with you, i starts at 0, so when we get to 7 (after 8 objects) it should stop.
3 Answers
Mustafa Dilaver
12,671 Pointsfor( int i=0; i < 8; i++) {
// Loop starts
//#1 Loop
i=0
//#2 Loop
i=1
//#3 Loop
i=2
//#4 Loop
i=3
//#5 Loop
i=4
//#6 Loop
i=5
//#7 Loop
i=6
//#8 Loop
i=7
}
//Loop ends
i = 8 //now
means that, increment "i" value by one when a loop FINISHED. At the last loop, because of another loop is finished, value of "i" gets incremented by one for one more time.
So it's value isn't 7, it is 8. But it doesn't throws an exception in the running program because after the last loop, we exit from the for loop and
days[8] = day;
never runs. It is a preview in the debugger.
Luke Liem
6,367 PointsYeah, got the same error.
Doing the following fix the error. Though I have no idea why the array went out of bound in the first place.
private Hour[] getHourlyForecast(String jsonData) throws JSONException { .....
Hour[] hourlyForecast = new Hour[data.length()+1];
.... }
private Day[] getDailyForecast(String jsonData) throws JSONException { ......
Day[] dailyForecast = new Day[data.length()+1];
...... }
Michael Van Dusen
12,136 PointsI got the same error and Luke Liem's fix did not work. After combing through the code with debugger I found that on line 180 I had. Day[] days = new Day[daily.length()];
It should be Day[] days = new Day[data.length()];
If you are getting this error check out this line and see if you have done the same thing!
Luiz Carlos
15,714 PointsLuiz Carlos
15,714 PointsPost your code here.