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

Yahya Tioso
Yahya Tioso
2,503 Points

Error on running Interactive Story. Please help me to fix it.

I have followed your video to fix it. But it can't work and keep give me same error. I already clean and rebuild project. And I also download your file and check one by one on java files to match my android studio. I have no idea what is wrong. Can you please help to fix it? Thank you

FATAL EXCEPTION: main java.lang.NullPointerException at yahyatioso.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:62) at yahyatioso.com.interactivestory.ui.StoryActivity.access$100(StoryActivity.java:18) at yahyatioso.com.interactivestory.ui.StoryActivity$3.onClick(StoryActivity.java:88)

4 Answers

Muzammil Ali
Muzammil Ali
5,966 Points

In case it's still relevant, you need to move
mChoice1.setText(mCurrentPage.getChoice1().getText()); mChoice2.setText(mCurrentPage.getChoice2().getText()); inside of the else brackets. It should work then.

those are the line numbers (62, 18 etc...) try putting them here not just those lines but code around.. so that we know what is happening..

Yahya Tioso
Yahya Tioso
2,503 Points

Hi. I am still newbie. Here's code in StoryActivity.java. I have given mark -> Line 62, -> Line 18 and -> Line 80. Please help me. Thanks

package yahyatioso.com.interactivestory.ui;

import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v7.app.ActionBarActivity; 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 yahyatioso.com.interactivestory.R; import yahyatioso.com.interactivestory.model.Page; import yahyatioso.com.interactivestory.model.Story;

public class StoryActivity extends ActionBarActivity { -> Line 18

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

    mChoice1.setText(mCurrentPage.getChoice1().getText());   -> Line 62
    mChoice2.setText(mCurrentPage.getChoice2().getText());

    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.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int nextPage = mCurrentPage.getChoice1().getNextPage();
                loadPage(nextPage); -> Line 80
            }
        });

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

}

Jake Koenig
Jake Koenig
8,448 Points

Muzammil is right, that's the exact problem I had, and the fix I used.