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 a Blog Reader Android App Using Intents to Display and Share Posts Opening a Webpage Within the App

NullPointerException in BlogWebViewActivity

I'm having trouble with showing the blog contents in the WebView. The program crashes with the above exception.

I've tried to use the debugger but I'm getting nowhere with that - the error is hidden.

I commented out the newly added code that caused the issue and then added the lines back one-by-one whilst adding a Log.e to see where I'm getting up to.

The NullPointerException is thrown by

webView.loadUrl(blogUri.toString());

I've output the contents of blogUri.toString() in the console - this is fine; a valid web address is displayed, i.e. not null.

I've tried this in the emulator and on a device with the same result. I've also amended my doInBackGround method as described in a forum post somewhere - same issue persists.

Any help would be appreciated!

Cheers,

Steve.

3 Answers

The URI and WebView are created with:

Uri blogUri = intent.getData();
WebView webView = (WebView) findViewById(R.id.webView1);

I think that's correct?

In that case, I would verify that you're first loading the correct layout with setContentView() and that it contains a WebView with the id @+id/webView1.

The only other pointer in that line is the webView. Are you sure it's properly assigned? Otherwise, I'd have to see the backtrace in your logcat to see what's going wrong.

I have

setContentView(R.layout.activity_blog_web_view);

that's the same name as the .xml file.

However! The webView1 is in a fragment, not the activity - that'll cause problems!

corrects cock-up

tests

THANK YOU!!! :-)

That now works fine.

Steve.