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

i need help in my project

am new in android studio and i get this proplem when i rghait this code TimePicker TP = (TimePicker) findViewById(R.id.timePicker); TP.getCurrentHour(); . the proplem is : Method invocation 'TP.getCurrentHour()' may produce 'java.lang.NullPointerException' less... (Ctrl+F1) This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations. Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report possible NullPointerException errors. More complex contracts can be defined using @Contract annotation, for example: @Contract(", null -> null") — method returns null if its second argument is null @Contract(", null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise @Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it The inspection can be configured to use custom @Nullable @NotNull annotations (by default the ones from annotations.jar will be used

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Android Studio does some automatic code inspection, beyond the usual syntax checking. It's a warning from Android Studio that it might be a cause of an runtime error when you run your app, but not necessarily. I've found the "may cause a NullPointerException" one to be hit or miss personally. (Flags statements that would cause it rarely, if ever. Misses ones that actually cause NPEs).

thanks