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

Ragesh Kanagaraj
Ragesh Kanagaraj
3,637 Points

Unfortunately app stopped working.Null Pointer exception Issue in Oncreate() and StartStory

When I enter my Name and proceed with the button I get this usual error Unfortunately app Stopped Working.

My Code :

StartStory.java
          public class StartStory extends ActionBarActivity {

    private static final String TAG=StartStory.class.getSimpleName();
    private ImageView mImageView;
    private TextView mTextView;
    private Button mChoice1 , mChoice2;
    private Story mStory;
    private String mName;
         protected void onCreate(Bundle savedInstanceState) 
           {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_story_layout);
        Intent intent=getIntent();
         mName=intent.getStringExtra(getString(R.string.intentKey));
       // Log.d(TAG,mName);
        if(mName.isEmpty()){
            mName="Friend";
        }
        mImageView=(ImageView) findViewById(R.id.idImageView);
        mTextView=(TextView) findViewById(R.id.idTextView);
        mChoice1=(Button) findViewById(R.id.idChoiceOne);
        mChoice2=(Button) findViewById(R.id.idChoiceTwo);
        loadPage();

         }

       private void loadPage() {

       Page page=mStory.returnPage(0);

        Drawable drawable=getResources().getDrawable(page.getImageId());
        mImageView.setImageDrawable(drawable);
        mTextView.setText(String.format(page.getText(), mName));
        mChoice1.setText(page.getMchoice1().getText());
        mChoice2.setText(page.getMchoice2().getText());
    }

Logs:

       Process: com.example.ragesh.mystory, PID: 9696
        java.lang.RuntimeException: Unable to start activity              ComponentInfo{com.example.ragesh.mystory/com.example.ragesh.mystory.View.StartStory}:    java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.ragesh.mystory.Model.Page com.example.ragesh.mystory.Model.Story.returnPage(int)' on  a    null object reference
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method       'com.example.ragesh.mystory.Model.Page com.example.ragesh.mystory.Model.Story.returnPage(int)' on a     null object reference
                 at com.example.ragesh.mystory.View.StartStory.loadPage(StartStory.java:46)
               at com.example.ragesh.mystory.View.StartStory.onCreate(StartStory.java:39)
               at android.app.Activity.performCreate(Activity.java:5933)
               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                  at android.app.ActivityThread.access$800(ActivityThread.java:144)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Dan Johnson
Dan Johnson
40,532 Points

Did you assign a new Story object to mStory when you declared it?

I agree with Dan. Your mStory is null so the following line is throwing an error:

Page page = mStory.returnPage(0);

Where do you assign something to mStory?

Steve.

Ragesh Kanagaraj
Ragesh Kanagaraj
3,637 Points

Yes i had declared it. Forgot to attach that part of my code here. I have edited my question now I have added that part as well.

Harry James
Harry James
14,780 Points

Hey Ragesh!

As Dan Johnson has pointed out - you need to initialize the mStory variable (See his answer for how to do this).

Right now, you have declared the variable but, it isn't set to anything (So, it's null) which is is why you're getting the error.

1 Answer

Dan Johnson
Dan Johnson
40,532 Points

You'll need to create a Story object either in onCreate or with the declaration so mStory references an actual object:

private Story mStory = new Story();

onCreate option:

mStory = new Story();