Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Self-Destructing Message Android App Capturing Photos and Videos Selecting a Video from the Gallery

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

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.