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 Self-Destructing Message Android App Capturing Photos and Videos Selecting a Video from the Gallery

P M
P M
7,320 Points

Warning about 'java.lang.NullPointerException' on inputStream.close() in try block

Even when wrapped in the try block, the line inputStream.close() produces a warning: Method invocation 'inputStream.close()' may produce 'java.lang.NullPointerException' in Android Studio. It doesn't in the video, and I find it strange since it is already wrapped in the exception handling block. Why does that warning appear, and should any special precautions be taken against the NullPointerException?

1 Answer

There could be a number of possibilities for a method to throw a Null Pointer exception warning. Usually a very open debate since Dalvik (Android Virtual Machine) may assert its own means.

It could also be for the fact that NullPointerException is an unchecked exception (exceptions that need not be included in any method’s throws list). Also the reason why wrapping it up in try catch block might still produce a warning

Best way could be to check at intervals if your data != null or cause conditions that might lead to early failure of the program but gives you vital information of what's happening.

I usually have a lot of console print statements (System.out.print) when I write a program. Just helps me keep a check at regular intervals of whats happening

But if I do find an alternative or a better explanation to your question I'll get back

P M
P M
7,320 Points

Thanks! I added a catch NullPointerException clause, but the warning was still there. Wrapping the close() calls in a null check removed the warning.