When calling the retrofit method, a "java.lang.NullPointerException" may occur

Using Retrofit 2.3.0 I get the following message in Android Studio

enter image description here

Any suggestions for removing this IDE error message. Thanks

+4
source share
1 answer

From the answer documentation :

@Nullable
public T body()

From a deserialized response body to a successful response.

This means that it response.body()can return null, and as a result, the call response.body().getItems()can call NullPointerException. To avoid a warning, be sure response.body() != nullto call the methods on it before.

Edit

, , . :

mAdapter.addItems(response.body().getItems());

:

if (response.body() != null) {
    mAdapter.addItems(response.body().getItems());
}

(, ) , response.body() , . , :

MyClass body = response.body();
if (body != null) {
    mAdapter.addItems(body.getItems());
}
+9

Source: https://habr.com/ru/post/1682688/


All Articles