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) Updating the Data Model From JSONArray to a Java Array

Maximilian Bertram
Maximilian Bertram
9,486 Points

Why is my different approach on getHourlyForecast not working?

Hey there, as Ben recommended, i tried the conversion of the JSONArray object into a String Array first on my own. I nearly had the same code that Ben has with just one minor difference which caused my app to crash and give me a java.lang.NullPointerException message in the log. All I did different was not creating a new Hour object in the for loop like Ben did (Hour[] hour = new Hour[]) and instead just trying to set the JSON data directly to the appropriate value of the already existing Hour Array called "hours", just like so: "hours[i].setTime(jsonHour.getLong("time"));" for example. I don't understand why this is not working. Thanks for any answer and help, Max

2 Answers

Ok so when you instantiate a new array like:

// lets just say data.length() returns 5
Hour[] hours = new Hour[data.length()]

Whats happening is it creates a new array that looks like hours[null, null, null, null, null], because while you've set the length you havent set any elements in the array. So then, when you call hours[i].setTime(), your invoking that method on a null object. Try this:

            Hour hour = new Hour();

            hour.setSummary(jsonHour.getString("summary"));
            hour.setTemperature(jsonHour.getDouble("temperature"));
            hour.setIcon(jsonHour.getString("icon"));
            hour.setTime(jsonHour.getLong("time"));
            hour.setTimezone(timezone);

            hours[i] = hour;
Maximilian Bertram
Maximilian Bertram
9,486 Points

ah okay thank your orbit, now I can understand why my code wasn't working! :)

Whats the full NPE error? It will tell you exactly what is null. Im going to guess that your not instantiating hours anywhere, the setTime() method is trying to be called on a null object, hence the error. Also, you mention Hour[] hour = new Hour[] but your calling hours[i].setTime(jsonHour.getLong("time"));,"hours" with an s

Maximilian Bertram
Maximilian Bertram
9,486 Points

Okay now i have a link to a screenshot of my code since I'm not familiar with this forum yet, I had no idea how to include it. Sorry for the inconvenience here is the link: http://imgur.com/vWbAZp6 So my full error looked like this: http://imgur.com/tyMFl8d Thank you for your fast answer though, maybe those two picks can clarify what I mean :)