Phate01 Phate01 - 5 months ago 56
Java Question

findViewById() may produce NullPointerException

I have many of these calls:

(ListView) getView().findViewById(;
(TextView) getView().findViewById(;

and AndroidStudio tells me that they may procude a

Method invocation
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
are treated as nullable
(or not-null, respectively) and used during the analysis to check
nullability contracts, e.g. report possible


More complex contracts can be defined using

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
method which throws an exception if true is passed to it

The inspection can be configured to use custom

annotations (by default the ones from annotations.jar will be used)

Luckily everithing works, but is there an improvement to this code I can made?

Answer Source

This is a known issue in and it has been fixed in v24.

You won't have any issues with or

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download