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 an Interactive Story App (Retired) Finishing the User Interface Ending the Story

NullPointer exception

So I've got the Interactive story working except one thing. When I reach the end and I keep clicking where the invisible button would be then hit play again it gives me the "Unfortunately .. has stopped working".

Here's the Logcat stuff:

01-23 03:00:24.808    1144-1144/faye.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at faye.interactivestory.uI.StoryActivity$3.onClick(StoryActivity.java:93)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Here's the Story activity part:

package faye.interactivestory.uI;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import faye.interactivestory.R;
import faye.interactivestory.model.Page;
import faye.interactivestory.model.Story;

public class StoryActivity extends Activity {

    public final String TAG = StoryActivity.class.getSimpleName();

    private Story mStory = new Story();
    private ImageView mImageView;
    private TextView mTextView;
    private Button mChoice1;
    private Button mChoice2;
    private String mName;
    private Page mCurrentPage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story);

        Intent intent = getIntent();
        mName = intent.getStringExtra(getString(R.string.key_name));

        if(mName == null){
            mName = "friend";
        }

        Log.d(TAG, mName);

        mImageView = (ImageView)findViewById(R.id.storyImageView);
        mTextView = (TextView)findViewById(R.id.storyTextView);
        mChoice1 = (Button)findViewById(R.id.choiceButton1);
        mChoice2 = (Button)findViewById(R.id.choiceButton2);

        loadPage(0);
    }

    private void loadPage(int choice){
        mCurrentPage = mStory.getPage(choice);

        Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
        mImageView.setImageDrawable(drawable);

        String pageText = mCurrentPage.getText();
        // Adds name if placeholder included. Won't add if not included
        pageText = String.format(pageText, mName);
        mTextView.setText(pageText);

        if( mCurrentPage.isFinal() ){
            //mChoice2.setText(mCurrentPage.getChoice2().getText());
            mChoice1.setVisibility(View.INVISIBLE);
            mChoice2.setText("Play again!");

            mChoice1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

        }
        else {
            mChoice2.setText(mCurrentPage.getChoice2().getText());
            mChoice1.setText(mCurrentPage.getChoice1().getText());


            mChoice1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int nextPage = mCurrentPage.getChoice1().getNextPage();
                    loadPage(nextPage);
                }
            });

            mChoice2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int nextPage = mCurrentPage.getChoice2().getNextPage();***
                    loadPage(nextPage);

                    /*try {
                        int nextPage = mCurrentPage.getChoice2().getNextPage();
                        loadPage(nextPage);
                    } catch (NullPointerException e){
                        e.printStackTrace();
                    }*/
                }
            });
        }

    }


}

I've marked it with ***

I know it's a stack trace issue. and as you can see the commented bit out of it shows my attempt. Here's what I get in the logcat instead:

01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ java.lang.NullPointerException
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at faye.interactivestory.uI.StoryActivity$3.onClick(StoryActivity.java:97)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at android.view.View.performClick(View.java:4240)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at android.view.View$PerformClick.run(View.java:17721)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-23 03:08:47.520    1225-1225/faye.interactivestory W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)