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 Finishing the User Interface Loading the First Page

Reevo XFR
Reevo XFR
713 Points

App keeps crashing...

09-19 22:17:54.262 19774-19774/com.teamtreehouse.interactivestory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.teamtreehouse.interactivestory, PID: 19774 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamtreehouse.interactivestory/com.teamtreehouse.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.teamtreehouse.interactivestory.model.Page com.teamtreehouse.interactivestory.model.Story.getPage(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 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:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.teamtreehouse.interactivestory.model.Page com.teamtreehouse.interactivestory.model.Story.getPage(int)' on a null object reference at com.teamtreehouse.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:51) at com.teamtreehouse.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 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:866)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 


package com.teamtreehouse.interactivestory.model;

import com.teamtreehouse.interactivestory.R;

/**

  • Created by reevo on 9/18/2017. */

public class Story {

private Page[] pages;

public Story(){

    pages = new Page[7];

    pages[0] = new Page(R.drawable.page0,
                        R.string.page0,
                        new Choice(R.string.page0_choice1, 1),
                        new Choice(R.string.page0_choice2, 2));

    pages[1] = new Page(R.drawable.page1,
                        R.string.page1,
                        new Choice(R.string.page1_choice1, 3),
                        new Choice(R.string.page1_choice2, 4));

    pages[2] = new Page(R.drawable.page2,
                        R.string.page2,
                        new Choice(R.string.page2_choice1, 4),
                        new Choice(R.string.page2_choice2, 6));

    pages[3] = new Page(R.drawable.page3,
                        R.string.page3,
                        new Choice(R.string.page3_choice1, 4),
                        new Choice(R.string.page3_choice2, 5));

    pages[4] = new Page(R.drawable.page4,
                        R.string.page4,
                        new Choice(R.string.page4_choice1, 5),
                        new Choice(R.string.page4_choice2, 6));

    pages[5] = new Page(R.drawable.page5, R.string.page5);

    pages[6] = new Page(R.drawable.page6, R.string.page6);

}

public Page getPage(int pageNumber) {

    if(pageNumber >= pages.length){
        pageNumber = 0;
    }
    return pages[pageNumber];
}

}


package com.teamtreehouse.interactivestory.ui;

import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText;

import com.teamtreehouse.interactivestory.R;

public class MainActivity extends AppCompatActivity {

private EditText nameField;
private Button startButton;

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

    nameField = (EditText)findViewById(R.id.nameEditText);
    startButton = (Button)findViewById(R.id.startButton);

    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = nameField.getText().toString();
            startStory(name);
        }
    });
}

private void startStory(String name) {
    Intent intent = new Intent(this, StoryActivity.class);
    Resources resources = getResources();
    String key = resources.getString(R.string.key_name);
    intent.putExtra(key, name);
    startActivity(intent);
}

}


package com.teamtreehouse.interactivestory.ui;

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

import com.teamtreehouse.interactivestory.R; import com.teamtreehouse.interactivestory.model.Choice; import com.teamtreehouse.interactivestory.model.Page; import com.teamtreehouse.interactivestory.model.Story;

public class StoryActivity extends AppCompatActivity {

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

private Story story;
private ImageView storyImageView;
private TextView storyTextView;
private Button choice1Button;
private Button choice2Button;

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


    storyImageView = (ImageView)findViewById(R.id.storageImageView);
    storyTextView = (TextView)findViewById(R.id.storyTextView);
    choice1Button = (Button)findViewById(R.id.choice1Button);
    choice2Button = (Button)findViewById(R.id.choice2Button);


    Intent intent = getIntent();
    String name = intent.getStringExtra(getString(R.string.key_name));
    if (name == null || name.isEmpty()) {
        name = "Friend";
    }
    Log.d(TAG, name);
    loadPage(0);
}

private void loadPage(int pageNumber) {

    Page page = story.getPage(pageNumber);

    Drawable image = ContextCompat.getDrawable(this, page.getImageId());
    storyImageView.setImageDrawable(image);

}

I really don't know how to solve it ...

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

You declared the member variable story but you didn't initialize it in onCreate().

Reevo XFR
Reevo XFR
713 Points

FINALLY. THANK YOU SO MUCH. I lost 45 minutes finding the mistake. xD

how would you do that?

story = new story(); /// like this