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 trialJoshua O'Connor
Courses Plus Student 3,207 PointsApp crash due to NullPointerException
I clicked on one of the items in the list, the app crashed, and my log told me that I had a NullPointer Exception. I have found the code that it is apparent source
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blog_web_view);
Intent intent = getIntent();
Uri blogUri = intent.getData();
WebView webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl(blogUri.toString()); //this line with the apparent issue of "NullPointerException"
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blog_web_view, menu);
return true;
}
}
And this is the Log 05-12 16:44:41.675: E/AndroidRuntime(3278): FATAL EXCEPTION: main 05-12 16:44:41.675: E/AndroidRuntime(3278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JoshuaOConnor.blogreader/com.JoshuaOConnor.blogreader.BlogWebViewActivity}: java.lang.NullPointerException 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.ActivityThread.access$600(ActivityThread.java:130) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.os.Handler.dispatchMessage(Handler.java:99) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.os.Looper.loop(Looper.java:137) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.ActivityThread.main(ActivityThread.java:4745) 05-12 16:44:41.675: E/AndroidRuntime(3278): at java.lang.reflect.Method.invokeNative(Native Method) 05-12 16:44:41.675: E/AndroidRuntime(3278): at java.lang.reflect.Method.invoke(Method.java:511) 05-12 16:44:41.675: E/AndroidRuntime(3278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 05-12 16:44:41.675: E/AndroidRuntime(3278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 05-12 16:44:41.675: E/AndroidRuntime(3278): at dalvik.system.NativeStart.main(Native Method) 05-12 16:44:41.675: E/AndroidRuntime(3278): Caused by: java.lang.NullPointerException 05-12 16:44:41.675: E/AndroidRuntime(3278): at com.JoshuaOConnor.blogreader.BlogWebViewActivity.onCreate(BlogWebViewActivity.java:21) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.Activity.performCreate(Activity.java:5008) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 05-12 16:44:41.675: E/AndroidRuntime(3278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 05-12 16:44:41.675: E/AndroidRuntime(3278): ... 11 more
Any guidance on what I have done wrong would be very much appreciated
3 Answers
Andrew Nash
344 PointsAre you still having this problem?
It seems that either webView or blogUri is null, meaning either getData or findViewById is returning null. Do you have experience setting breakpoints? If so, a breakpoint on the line that is failing will allow you to see which is causing the problem.
Alternatively, you could change the single line
webView.loadUrl(blogUri.toString());
so that it looks like this:
webView.loadUrl(
blogUri.toString()
);
and then run it and see which line it is failing on. Narrowing it down to one or the other will help figure out what the issue is. If you can verify which one is null I'll do my best to help you figure out why.
Joshua O'Connor
Courses Plus Student 3,207 PointsThanks. By setting up the breakpoints I saw my mistake. I had accidentally set up the webView in the fragment_blog_web_view.xml then I just had to put the webView in its rightful place the activity_blog_web_view.xml
Wade Welsh
1,507 PointsThanks for sharing your solution. I had the same issue.