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

Error in StoryActivity for Interactive Story. Not sure how to fix the error.

I have the logchat below:

02-03 19:09:16.422 19856-19856/teamtreehouse.com.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: teamtreehouse.com.interactivestory, PID: 19856 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at teamtreehouse.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:64) at teamtreehouse.com.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:49)

I checked the other activities and model for errors but I could not find any. The text is not highlighted in red at all. The error is on line 64 mTextView.setText(pageText); and line 49 loadPage(0);

The app simply crashes after I put any name in regardless of length. It just crashes and before I was even unable to get a logchat error.

StoryAcitivity code:

package teamtreehouse.com.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 teamtreehouse.com.interactivestory.R; import teamtreehouse.com.interactivestory.model.Page; import teamtreehouse.com.interactivestory.model.Story;

public class StoryActivity extends Activity {

public static 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);
    mChoice1 = (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();
    // Add the name if placeholder included. Won't add if no placeholder
    pageText = String.format(pageText, mName);
    mTextView.setText(pageText);

    if (mCurrentPage.isFinal()) {
        mChoice1.setVisibility(View.INVISIBLE);
        mChoice2.setText("PLAY AGAIN");
        mChoice2.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) { finish(); }
        });

    }
    else {
        mChoice1.setText(mCurrentPage.getChoice1().getText());
        mChoice2.setText(mCurrentPage.getChoice2().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);
            }
        });
    }
}

}

5 Answers

Richard Ling
Richard Ling
8,522 Points

Perfect, thanks for this - downloaded the project and ran it. The problem seemed to be on the OnCreate method of your StoryActivity, this section:

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

The last line there should be setting mChoice2 not mChoice1. After I changed this, the app seemed to work fine :)

The error was being thrown in the loadPage method at this line:

mChoice2.setText(mCurrentPage.getChoice2().getText());

getChoice2() returned null, so getText() failed.

Richard Ling
Richard Ling
8,522 Points

Hi,

Did you manage to figure this out in the end? I'm not in a position to run any code at the moment, but it looks like your mTextView object reference might be null, at this line:

mTextView = (TextView) findViewById(R.id.storyTextView);

So this line would fail if mTextView was null:

mTextView.setText(pageText);

Does the TextView have the correct ID (storyTextView) in your layout file? If you're still having problems and the above doesn't help, are you able to post your other code files as well (layout and classes)?

Thanks, Rich

Hello Rich,

I found a typo for the storyTextView and ImageView in the activity_story.xml. I corrected both. It seems the same error still appears. I can post all the code files here; however, I can post it on Git hub. Please let me know if you need anything else.

(https://github.com/ARV105/InteractiveStory.git)

Thanks so much Richard. I knew it was some dumb typo somewhere. I went back to the lecture videos multiple times and tried to retrace my steps. It is always best to have a new pair of eyes look at the code since they always find something I missed.

Richard Ling
Richard Ling
8,522 Points

Haha, no problem - issues like these are almost always something simple, and I still do this all the time ;)

Natalia Wojtkowska
Natalia Wojtkowska
754 Points

In case someone makes the same typo I did - I was getting an error on the if isFinal() condition (in StoryActivity) and as it turned out, I spelled Boolean with a capital "B" in the "page" model, where you specify private boolean mIsFinal on line 11.