Skip to content Skip to sidebar Skip to footer

Why Android Studio Has This @nullable Thing?

I am just kind of curious about this weird thing that Android Studio is showing me. I was about to use the addView method when this tool-tip-like thingy pops up: I am quite confus

Solution 1:

View can be null because it is a reference type. But why an explicit @Nullable annotation?

In technical sense, yes, View is reference thus null is one of values. In the same sense, width and height arguments in one of overloads are of type int so technically you could pass 0, 2147483647 or -1 value as height, for example, but it probably wouldn't make any sense. The same way, passing null as a value, however syntactically valid, may or may not make sense. Some functions throw NullPointerException when passed a null value. @Nullable annotation means that null is acceptable value, and function will handle that:

Nullable:

Denotes that a parameter, field or method return value can be null.

When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.

When decorating a method, this denotes the method might legitimately return null.

This is a marker annotation and it has no specific attributes.

Post a Comment for "Why Android Studio Has This @nullable Thing?"