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

FileHelper.java is creating a nullpointerexception

Hi guys, I'm following the video lessons on creating a self destructing message app for android. I've got to the end of sending messages but now when I run the app it crashes and give a nullpointerexception at line 27 in the FileHelper.java class. Any suggestions? I'm stuck

3 Answers

Thanks for your help! I also have another strange problem.... When I click on the menu in the action bar it opens edit friends and logout as it should, however, if I press either of these options it opens the camera options on the action bar and cause the app to crash. Any suggestions?

EDIT problem solved in a later video about adding break; in to the code

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry for the confusion! Glad you worked through it. We intentionally leave in mistakes because you can sometimes learn by watching someone else make a mistake. Hope that was the case this time... :)

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I'm guessing that your mMediaUri variable is null when you call this line in RecipientsActivity.java:

byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);

Can you debug there and verify?

Hi Ben Jakuben
what would be the work around if mMediaUri is indeed null at that line? For some reason it becomes null after calling the camera Intent. Is it because the ribbit activity gets destroyed when the camera is called? or because we're using MediaStore.EXTRA_OUTPUT that mMediaUri returns as null? (Apologies en avance if I haven't adhered to the posting norms in this forum, my first post here)

EDIT: used onSaveInstanceState(Bundle outState) to save mMediaUri value so that it isn't null when the ribbit activity resurfaces after taking the picture

Hi Ben,

Please excuse my ignorance, I'm new at this. How do I debug upto there and verify? Also the app works as it should on the emulator but when I test it on a actual device it causes it to crash and gives a nullpointerexception at 'if (uri.getScheme().equals("content"))' in FileHelper.java

EDIT I figured out the debugger. I stopped it at byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri); and everything seemed fine so I let it run until the end with no errors.

now the app runs perfectly on both the emulator and device, but I have no idea why?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Haha - welcome to software development! These are the most frustrating issues--when you can't recreate them on demand. It's possible that something somewhere was stuck in the version on your device and has since been cleared with the latest build. But I'd test it a few times to make sure it doesn't pop back up. If this were full-fledged production app you would want to retest on other devices to see if you could recreate it.