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

Tom Finet
Tom Finet
7,027 Points

Error: Exception Caught

I am following the android weather app course and ran into an error at the end of the tutorial.

Here is the logcat log:

E/MainActivity: Exception caught org.json.JSONException: No value for precipChance at org.json.JSONObject.get(JSONObject.java:389) at org.json.JSONObject.getDouble(JSONObject.java:444) at com.example.tomfinet.stormy.MainActivity.getCurrentDetails(MainActivity.java:96) at com.example.tomfinet.stormy.MainActivity.access$100(MainActivity.java:22) at com.example.tomfinet.stormy.MainActivity$1.onResponse(MainActivity.java:66) at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168) at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

Here is the code most likely concerned:

MainActivity.java:

if (networkIsAvailable())
    {
        OkHttpClient client = new OkHttpClient(); //construct

        Request request = new Request.Builder()
                .url(forecastUrl)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e)
            {

            }

            @Override
            public void onResponse(Response response) throws IOException
            {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                    if (response.isSuccessful())
                    {
                        mCurrentWeather = getCurrentDetails(jsonData);
                    } else
                    {
                        alertUserAboutError();
                    }
                } catch (IOException e)
                {
                    Log.e(TAG, "Exception caught", e);
                } catch (JSONException e)
                {
                    Log.e(TAG, "Exception caught", e);
                }
            }
        });
    }

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException
{
    JSONObject forecast = new JSONObject(jsonData);
    String timezone = forecast.getString("timezone");
    Log.i(TAG, "From JSON: " + timezone);

    JSONObject currently = forecast.getJSONObject("currently");
    CurrentWeather currentWeather = new CurrentWeather();

    currentWeather.setHumidity(currently.getDouble("humidity"));
    currentWeather.setTime(currently.getLong("time"));
    currentWeather.setIcon(currently.getString("icon"));
    currentWeather.setPrecipChance(currently.getDouble("precipChance"));
    currentWeather.setSummary(currently.getString("summary"));
    currentWeather.setTemperature(currently.getDouble("temperature"));
    currentWeather.setTimeZone(timezone);

    Log.d(TAG, currentWeather.getFormattedTime());

    return currentWeather;
}

CurrentWeather.java:

private String mTimeZone;

public String getTimeZone() {
    return mTimeZone;
}

public void setTimeZone(String timeZone) {
    mTimeZone = timeZone;
}

public String getFormattedTime()
{
    SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
    formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
    Date dateTime = new Date(getTime() * 1000);
    String timeString = formatter.format(dateTime);
    return timeString;
}

Thank you.

If there is something missing please tell me and I will add it!

Quincy Leito
Quincy Leito
20,503 Points

The logcat already gives you the answer you are looking for. The value precipChance doesn't exist in the JSONObject. You need to look at what data you want to retrieve from the currently JSONObject.