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

"Interactive Story has stopped working" after clicking start your adventure

Im getting a weird error when I click the start adventure button after inputting a name.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jakec.interactivestory/com.example.jakec.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.jakec.interactivestory.model.Choice.getText()' on a null object reference

I'm guessing something is wrong with my choice.getText method but i made it the same way as in the videos. I have been trying to fix this for hours, any help would be appreciated.

2 Answers

I don't remember if they provide the full src to the project but here: https://github.com/rchatters/InteractiveStory/tree/master/app

My guess you may not be setting the text for it to get. https://github.com/rchatters/InteractiveStory/blob/master/app/src/main/java/com/rchatter/interactivestory/ui/StoryActivity.java

From the stack trace you have a null value someplace in your getText(). Without seeing your model, see if this matches your Choice model:

public class Choice {
    private String mText;
    private int mNextPage;

    public Choice(String text, int nextPage) {
        mText = text;
        mNextPage = nextPage;
    }

    public String getText() {
        return mText;
    }

    public void setText(String text) {
        mText = text;
    }

    public int getNextPage() {
        return mNextPage;
    }

    public void setNextPage(int nextPage) {
        mNextPage = nextPage;
    }
}

My Choice class matches, I even rewrote it to be sure. I was thinking that it could be something within the page class setting the text in the choice class as null but, I'm pretty sure my page class is correct as well.

public class Page { private int mImageId; private String mText; private Choice mChoice1; private Choice mChoice2; private boolean mIsFinal = false;

public Page(int imageId, String text, Choice choice1, Choice choice2) {
    mImageId = imageId;
    mText = text;
    mChoice1 = choice1;
    mChoice1 = choice2;
}

public boolean isFinal() {
    return mIsFinal;
}

public void setIsFinal(boolean isFinal) {
    mIsFinal = isFinal;
}

public Page(int imageId, String text) {
    mImageId = imageId;
    mText = text;
    mChoice1 = null;
    mChoice1 = null;
    mIsFinal = true;

}

public int getImageId() {
    return mImageId;
}

public String getText() {
    return mText;
}

public void setText(String text) {
    mText = text;
}

public void setImageId(int imageId) {
    mImageId = imageId;
}

public Choice getChoice1() {
    return mChoice1;
}

public void setChoice1(Choice choice1) {
    mChoice1 = choice1;
}

public Choice getChoice2() {
    return mChoice2;
}

public void setChoice2(Choice choice2) {
    mChoice2 = choice2;
}

}