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 Build a Weather App (2015) Hooking Up the Model to the View Wrapping Up

Raghav Singh
Raghav Singh
1,548 Points

Unfortunately, Story has stopped! error

The app doesn't even open. Shuts immediately!

5 Answers

Raghav Singh
Raghav Singh
1,548 Points

Solved it!

There is a problem with BindView

I imported the 7.0.1 version in the Gradle. and it works now.

Thanks for all the help Steve :)

Good work - well done! :+1:

Hi Raghav,

In your Logcat area in Android Studio, there will be details of the error that is causing the app to crash. If you look for messages that are generated by your app you should find them - there's likely to be a lot! If you can copy & paste all those into here, we can work out what is wrong.

Steve.

Raghav Singh
Raghav Singh
1,548 Points

Here you go Steve!

07-13 13:38:41.621 2786-2792/com.raghavsingh.stormy I/art: Debugger is activ
07-13 13:38:41.811 2786-2786/com.raghavsingh.stormy I/System.out: Debugger has connected
07-13 13:38:41.811 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:42.011 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:42.211 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:42.411 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:42.611 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:42.811 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:43.011 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:43.211 2786-2786/com.raghavsingh.stormy I/System.out: waiting for debugger to settle...
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy I/System.out: debugger has settled (1427)
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy I/InjectionManager: Inside getClassLibPath caller 
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy W/System: ClassLoader referenced unknown path: /data/app/com.raghavsingh.stormy-2/lib/arm64
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy D/InjectionManager: InjectionManager
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy D/InjectionManager: fillFeatureStoreMap com.raghavsingh.stormy
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy I/InjectionManager: Constructor com.raghavsingh.stormy, Feature store :{}
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy I/InjectionManager: featureStore :{}
07-13 13:38:43.411 2786-2786/com.raghavsingh.stormy W/ResourcesManager: getTopLevelResources: /data/app/com.raghavsingh.stormy-2/base.apk / 1.0 running in com.raghavsingh.stormy rsrc of package com.raghavsingh.stormy
07-13 13:38:43.421 2786-2786/com.raghavsingh.stormy W/ResourcesManager: getTopLevelResources: /data/app/com.raghavsingh.stormy-2/base.apk / 1.0 running in com.raghavsingh.stormy rsrc of package com.raghavsingh.stormy
07-13 13:38:43.511 2786-2786/com.raghavsingh.stormy D/AndroidRuntime: Shutting down VM
07-13 13:38:43.521 2786-2786/com.raghavsingh.stormy E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.raghavsingh.stormy, PID: 2786
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raghavsingh.stormy/com.raghavsingh.stormy.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                          at com.raghavsingh.stormy.MainActivity.onCreate(MainActivity.java:59)
                                                                          at android.app.Activity.performCreate(Activity.java:6876)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:158) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Raghav Singh
Raghav Singh
1,548 Points

Here is the MainActivity code

package com.raghavsingh.stormy;

        import android.app.Activity;
        import android.content.Context;
        import android.graphics.drawable.Drawable;
        import android.net.ConnectivityManager;
        import android.net.NetworkInfo;
        import android.os.Bundle;
        import android.support.v4.content.res.ResourcesCompat;
        import android.support.v7.app.ActionBarActivity;
        import android.util.Log;
        import android.view.View;
        import android.widget.ImageView;
        import android.widget.ProgressBar;
        import android.widget.TextView;
        import android.widget.Toast;

        import okhttp3.Call;
        import okhttp3.Callback;
        import okhttp3.OkHttpClient;
        import okhttp3.Request;
        import okhttp3.Response;

        import org.json.JSONException;
        import org.json.JSONObject;

        import java.io.IOException;

        import butterknife.ButterKnife;
        import butterknife.BindView;


public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

    private CurrentWeather mCurrentWeather;

    @BindView (R.id.timeLabel) TextView mTimeLabel;
    @BindView (R.id.temperatureLabel) TextView mTemperatureLabel;
    @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_main);
        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);

        Log.d(TAG, "Main UI code is running!");
    }

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

        if (isNetworkAvailable()) {
            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();
                        }
                    });
                    alertUserAboutError();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                        }
                    });

                    try {
                        String jsonData = response.body().string();
                        Log.v(TAG, jsonData);
                        if (response.isSuccessful()) {
                            mCurrentWeather = getCurrentDetails(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 {
            Toast.makeText(this, getString(R.string.network_unavailable_message),
                    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() {
        mTemperatureLabel.setText(mCurrentWeather.getTemperature() + "");
        mTimeLabel.setText("At " + mCurrentWeather.getFormattedTime() + " it will be");
        mHumidityValue.setText(mCurrentWeather.getHumidity() + "");
        mPrecipValue.setText(mCurrentWeather.getPrecipChance() + "%");
        mSummaryLabel.setText(mCurrentWeather.getSummary());

        Drawable drawable = ResourcesCompat.getDrawable(getResources(),mCurrentWeather.getIconId(),null);
        mIconImageView.setImageDrawable(drawable);
    }

    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("precipProbability"));
        currentWeather.setSummary(currently.getString("summary"));
        currentWeather.setTemperature(currently.getDouble("temperature"));
        currentWeather.setTimeZone(timezone);

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

        return currentWeather;
    }


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

        return isAvailable;
    }

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

OK - cool. So, this shows that you've got a NullPointerException at Line 59 of MainActivity, which is inside your onCreate method.

Can you point out which line is line 59 please? I go cross-eyed trying to count lines on the screen!

Steve.

I amended your code block to display better and hid your API key ...

I'm off to work now so I'll pick this up when I get home. I should only be a few hours.

Raghav Singh
Raghav Singh
1,548 Points
protected void onCreate(Bundle savedInstanceState) 

This is the line showing the current error once I put

mProgressBar.setVisibility(View.INVISIBLE); 

as a comment. If I don't put it in comments then it shows the same error for this ProgressBar line.