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

JSON Error in String timezone in both the Hour and Daily method

Here i added code that throws an JSONException for the String timezone (now this only happens in the Array containing methods which should have not occurred as the code is completely correct. i have also debugging which ultimately shows up the error in new Hour[] declartion,

JSON Error

         03-10 00:37:27.645 3257-3299/niketan.developer.com.stormy E/StormyMainActivity: Exception Caught
                                                                            org.json.JSONException: No value for America/Los_Angeles
                                                                                at org.json.JSONObject.get(JSONObject.java:389)
                                                                                at org.json.JSONObject.getString(JSONObject.java:550)
                                                                                at                  
          niketan.developer.com.stormy.UI.StormyMainActivity.getDailyWeatherDetails(StormyMainActivity.java:151)
                                                                                at           
          niketan.developer.com.stormy.UI.StormyMainActivity.parseForecastDetails(StormyMainActivity.java:191)
                                                                                at   
          niketan.developer.com.stormy.UI.StormyMainActivity.access$300(StormyMainActivity.java:33)
                                                                                at  
          niketan.developer.com.stormy.UI.StormyMainActivity$2.onResponse(StormyMainActivity.java:110)
                                                                                at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
                                                                                at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                                at    
          java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                at 
          java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                at java.lang.Thread.run(Thread.java:818)

StormyMainActivity

        private Hour[] getHourWeatherDetails(String jsonData) throws JSONException {
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");

        JSONObject hourly = forecast.getJSONObject("hourly");
        JSONArray data = hourly.getJSONArray("data");

        Log.i(TAG, "From Hour Method" + data);

        Hour[] hours = new Hour[data.length()];

        for (int i = 0; i < data.length(); i++){
        JSONObject jsonHour = data.getJSONObject(i);

        Hour hour = new Hour();
        hour.setIcon(jsonHour.getString("icon"));
        hour.setTemperature(jsonHour.getDouble("temperature"));
        hour.setSummary(jsonHour.getString("summary"));
        hour.setTime(jsonHour.getLong("time"));
        hour.setTimeZone(jsonHour.getString(timezone));

        Log.i(TAG, "From Hour" + hour);

        hours[i] = hour;
    }
    return hours;
}

(same goes with Daily Array method)

if you guys can find something that is causing this error please do let me know

1 Answer

Roxana Jula
Roxana Jula
2,753 Points

Hi, You should have hour.setTimezone(timezone); instead of hour.setTimeZone(jsonHour.getString(timezone));

The timezone value is found in the forecast JSON Object, not inside of hourly/daily JSON Objects. So we save it at the beginning of the method so we can access it when we need it.

Roxana Jula
Roxana Jula
2,753 Points

The value of timezone is "America/Los_Angeles" So when you are writing hour.setTimeZone(jsonHour.getString(timezone)); is like you are writing hour.setTimeZone(jsonHour.getString("America/Los_Angeles")); Do you see the issue?