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
Tonnie Fanadez
UX Design Techdegree Graduate 22,767 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 :)