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 Formatting Strings

Shivam Selam
Shivam Selam
624 Points

My app crashes when i run it! i believe it has multiple errors.

i have been trying to fix my errors using Android studios help and now im left with what looks like a clean code but nothing actually works. It worked fine once when i inserted the loadpage() inside the if statement. but still the the page didnt get updated and the two choice buttons were not working

Shivam Selam
Shivam Selam
624 Points

this is the StoryActivity.java

import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

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

public class StoryActivity extends AppCompatActivity {

public Story mStory;
private ImageView mImageView;
private TextView mTextView;
private Button mChoice1;
private Button mChoice2;
private String mName;


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

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

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

    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();

}

    private void loadPage(){
    Page page = mStory.getPage(0);

        Drawable drawable = getResources().getDrawable(page.getImageId(), null);
        mImageView.setImageDrawable(drawable);

        String pageText = page.getText();
        pageText = String.format(pageText, mName);
        // add name if placeholder included
        mTextView.setText(pageText);

        mChoice1.setText(page.getChoice1().getText());
        mChoice2.setText(page.getChoice2().getText());
}

}

Shivam Selam
Shivam Selam
624 Points

this is my Story.java

public class Story {

private Page[] mPages;

public Story(Page[] pages) {
    mPages = pages;

    mPages[0] = new Page(
            R.mipmap.page0,
            "On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: \"Help me, %1$s, you're my only hope.\"",
            new Choice("Stop and investigate", 1),
            new Choice("Continue home to Earth", 2));

    mPages[1] = new Page(
            R.mipmap.page1,
            "You deftly land your ship near where the distress signal originated. You didn't notice anything strange on your fly-by, but there is a cave in front of you. Behind you is an abandoned rover from the early 21st century.",
            new Choice("Explore the cave", 3),
            new Choice("Explore the rover", 4));

    mPages[2] = new Page(
            R.mipmap.page2,
            "You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low.",
            new Choice("Head back to Mars to investigate", 4),
            new Choice("Continue home to Earth", 6));

    mPages[3] = new Page(
            R.mipmap.page3,
            "Your EVA suit is equipped with a headlamp, which you use to navigate the cave. After searching for a while your oxygen levels are starting to get pretty low. You know you should go refill your tank, but there's a very faint light up ahead.",
            new Choice("Refill at ship and explore the rover", 4),
            new Choice("Continue towards the faint light", 5));

    mPages[4] = new Page(
            R.mipmap.page4,
            "The rover is covered in dust and most of the solar panels are broken. But you are quite surprised to see the on-board system booted up and running. In fact, there is a message on the screen: \"%1$s, come to 28.543436, -81.369031.\" Those coordinates aren't far, but you don't know if your oxygen will last there and back.",
            new Choice("Explore the coordinates", 5),
            new Choice("Return to Earth", 6));

    mPages[5] = new Page(
            R.mipmap.page5,
            "After a long walk slightly uphill, you end up at the top of a small crater. You look around, and are overjoyed to see your favorite android, %1$s-S1124. It had been lost on a previous mission to Mars! You take it back to your ship and fly back to Earth.");

    mPages[6] = new Page(
            R.mipmap.page6,
            "You arrive home on Earth. While your mission was a success, you forever wonder what was sending that signal. Perhaps a future mission will be able to investigate...");
}

public Page getPage(int pageNumber) {
    return mPages[pageNumber];
}

}

Seth Kroger
Seth Kroger
56,414 Points

It would be a big help it you looked at the stacktrace for the crash to find out where in you code the crash occurs and what exception caused the crash. You'll find it in the logcat.

3 Answers

Seth Kroger
Seth Kroger
56,414 Points

In the Story constructor, you are passing in the array of pages, but it looks like that array is null, hence the NullPointerException. That array has to be created with new Page[x]; somewhere. Because the Pages are part of the Story and there's a set number of them, you can remove the Page[] parameter from the constructor and just set mPages = new Page[7];

Shivam Selam
Shivam Selam
624 Points

Alright here's the logcat:

FATAL EXCEPTION: main Process: sshivamstan.interactivestory, PID: 3493 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{sshivamstan.interactivestory/sshivamstan.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to write to null array at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to write to null array at sshivamstan.interactivestory.model.Story.<init>(Story.java:12) at sshivamstan.interactivestory.ui.StoryActivity.<init>(StoryActivity.java:21) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  Application terminated.

Shivam Selam
Shivam Selam
624 Points

Well I did what you told and now the "Start Your Adventure" button works the Page(0) is not showing up as described in the Story.java and The two choice buttons not working.