Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Lists and Adapters (2015) Using Parcelable Data Retrieving Parcelable Data

samuel zaffran
samuel zaffran
24,815 Points

Double Errors

Hi, i have two issues, first when i launch my app, i have :

09-12 14:38:18.511 9822-9864/com.example.ideo.weather E/WeatherHome: exception caught org.json.JSONException: Value hourly of type java.lang.String cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:111) at org.json.JSONObject.<init>(JSONObject.java:160) at org.json.JSONObject.<init>(JSONObject.java:173) at com.example.ideo.weather.UI.WeatherHome.getHourlyForecast(WeatherHome.java:202) at com.example.ideo.weather.UI.WeatherHome.parseForecastDetails(WeatherHome.java:165) at com.example.ideo.weather.UI.WeatherHome.access$300(WeatherHome.java:38) at com.example.ideo.weather.UI.WeatherHome$2.onResponse(WeatherHome.java:109) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:126) 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)

Then, when i click on 7 days, i have :

09-12 14:40:01.041 9822-9822/com.example.ideo.weather E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ideo.weather, PID: 9822 java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.ideo.weather.weather.Day[] com.example.ideo.weather.weather.Forecast.getDailyForecast()' on a null object reference at com.example.ideo.weather.UI.WeatherHome.startDailyActivity(WeatherHome.java:260) at com.example.ideo.weather.UI.WeatherHome_ViewBinding$1.doClick(WeatherHome_ViewBinding.java:40) at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Don't understand, i search on the forum, found nothing. I posted my main code below, if you need anything else, tell me. Thank you !

samuel zaffran
samuel zaffran
24,815 Points

public class WeatherHome extends AppCompatActivity {

public static final String TAG = WeatherHome.class.getSimpleName();
public static final String DAILY_FORECAST = "DAILY_FORECAST";

private Forecast mForecast;

@BindView(R.id.temperatureLabel) TextView mTemperatureLabel;
@BindView(R.id.timeLabel) TextView mTimeLabel;
@BindView(R.id.humidityValue) TextView mHumidityValue;
@BindView(R.id.precipValue) TextView mPrecipValue;
@BindView(R.id.summaryLabel) TextView mSummaryLabel;
@BindView(R.id.iconImageView) ImageView mIconImageView;
@BindView(R.id.refreshImageView) ImageView mRefreshImageView;
@BindView(R.id.progressBar) ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_weather_home);
    ButterKnife.bind(this);

    mProgressBar.setVisibility(View.INVISIBLE);

    final double latitude = 37.8267;
    final double longitude = -122.423;

    mRefreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getForecast(latitude, longitude);

        }
    });

    getForecast(latitude, longitude);
}

private void getForecast(double latitude, double longitude) {
    String apiKey = "f16fc6fc8b09f4cacd3861ea68e99e9f";
    String forecastURL = "https://api.forecast.io/forecast/" + apiKey + "/" + latitude +
            "," + longitude;

    if (isNetworkisAvailable()) {
        toggleRefresh();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(forecastURL).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       toggleRefresh();
                   }
               });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                        }
                    });
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData );
                    if (response.isSuccessful()) {
                        mForecast = parseForecastDetails(jsonData);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateDisplay();
                            }
                        });
                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "exception caught", e);
                }
                  catch (JSONException e){
                      Log.e(TAG, "exception caught", e);
                  }
            }
        });


    }

    else {
        alertUserAboutError();
        Toast.makeText(this, "Network not available", Toast.LENGTH_LONG).show();
    }
}

private void toggleRefresh() {
    if(mProgressBar.getVisibility() == View.INVISIBLE){
        mProgressBar.setVisibility(View.VISIBLE);
        mRefreshImageView.setVisibility(View.INVISIBLE);
    } else {
        mProgressBar.setVisibility(View.INVISIBLE);
        mRefreshImageView.setVisibility(View.VISIBLE);
    }

}

private void updateDisplay() {
    Current current = mForecast.getCurrent();
    mTemperatureLabel.setText(current.getTemp() + "");
    mTimeLabel.setText("At " + current.getFormattedTime() + " it will be");
    mHumidityValue.setText(current.getHumidity() + "");
    mPrecipValue.setText(current.getPrecip() + "");
    mSummaryLabel.setText(current.getSummary());

    Drawable drawable = getResources().getDrawable(current.getIconId());
    mIconImageView.setImageDrawable(drawable);

}

private Forecast parseForecastDetails(String jsonData) throws JSONException{
    Forecast forecast = new Forecast();

    forecast.setCurrent(getCurrentDetails(jsonData));
    forecast.setHourlyForecast(getHourlyForecast(jsonData));
    forecast.setDailyForecast(getDailyForecast(jsonData));

    return forecast;
}

private Day[] getDailyForecast(String jsonData) throws JSONException {
    JSONObject forecast = new JSONObject(jsonData);
    String timeZone = forecast.getString("timezone");

    JSONObject daily = new JSONObject("daily");
    JSONArray data = daily.getJSONArray("data");

    Day[] dailies = new Day[data.length()];

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

        JSONObject jsonDay = new JSONObject("daily");
        Day day = new Day();

        day.setSummary(jsonDay.getString("summary"));
        day.setTime(jsonDay.getLong("time"));
        day.setTimeZone(timeZone);
        day.setTempMax(jsonDay.getDouble("temperatureMax"));
        day.setIcon(jsonDay.getString("icon"));

        dailies[i] = day;

    }

    return dailies;
}

private Hourly[] getHourlyForecast(String jsonData) throws JSONException{
    JSONObject forecast = new JSONObject(jsonData);
    String timeZone = forecast.getString("timezone");

    JSONObject hourly = new JSONObject("hourly");
    JSONArray data = hourly.getJSONArray("data");

    Hourly[] hourlies = new Hourly[data.length()];

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

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

        hourlies[i] = hour;
    }

    return hourlies;
}

private Current getCurrentDetails(String jsonData) throws JSONException {
    JSONObject forecast = new JSONObject(jsonData);
    String timeZone = forecast.getString("timezone");

    JSONObject currently = forecast.getJSONObject("currently");

    Current current = new Current();
    current.setHumidity(currently.getDouble("humidity"));
    current.setTime(currently.getLong("time"));
    current.setIcon(currently.getString("icon"));
    current.setPrecip(currently.getDouble("precipProbability"));
    current.setSummary(currently.getString("summary"));
    current.setTemp(currently.getDouble("temperature"));
    current.setTimezone(timeZone);

    return current;
}

private boolean isNetworkisAvailable() {
    ConnectivityManager manager = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    boolean isAvailable = false;
    if(networkInfo.isAvailable() && networkInfo.isConnected()){
        isAvailable = true;
    }
    return isAvailable;
}

private void alertUserAboutError() {
    AlertDialogFragment alertDialog = new AlertDialogFragment();
    alertDialog.show(getFragmentManager(), "Error_dialog");
}

@OnClick(R.id.dailyButton)
public void startDailyActivity(View view){
    Intent intent = new Intent(this, DailyForecastActivity.class);
    intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast());
    startActivity(intent);
}

}

1 Answer

The methods that have the errors are getHourlyForecast and getDailyForecast

When you are retrieving the "daily" jsonobject from within the forecast object you need to use forecast.getJSONObject("daily") instead of JSONObject daily = new JSONObject("daily")

Same thing goes with the hourly one too.