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

app is crashing

Getting this error when i run the app and app is crashing::

2019-02-13 15:23:23.375 25794-25794/com.yogigorle.interactivestory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.yogigorle.interactivestory, PID: 25794 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yogigorle.interactivestory/com.yogigorle.interactivestory.Ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference at com.yogigorle.interactivestory.Ui.StoryActivity.loadPage(StoryActivity.java:60) at com.yogigorle.interactivestory.Ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:6857) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:163)  at android.app.ActivityThread.main(ActivityThread.java:6238)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hi bohot hard it would help if we can see your code.

You can use free Gyazo application to take a snapshot of your code and then generate a link to the image which you can paste the link here.

Download and install Gyazo from this link.

https://gyazo.com/download?dl=now

1 Answer

Hi. If you look at the "Caused by" part in your log:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference at com.yogigorle.interactivestory.Ui.StoryActivity.loadPage(StoryActivity.java:60) at com.yogigorle.interactivestory.Ui.StoryActivity.onCreate(StoryActivity.java:46)

you see that you have a null ImageView instance on which you're calling setImageDrawable() ("invoke method (...) on a null object reference"). It's telling you to check lines 60 and 46 of StoryActivity.java.

Make sure you called findViewById() properly for storyImageView and that you have the correct imports for the image:

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

If that doesn't help, then paste your code here as Tonnie Fanadez suggested so we can help you better. Thanks :)