Method invocation may produce NullPointerException Retrofit Body

Priya :

I am using Retrofit 2 for get response from my API and store its value in my constant like below

if(response.isSuccessful()) {
                    constant.banner_on = response.body().getBanner_on();
                    constant.int_on = response.body().getInt_on();
                    constant.int_click = response.body().getInt_click();
                }

its giving me warning on all three like below

Method invocation getBanner_on may produce java.lang.nullPointerException

I am confused and unable to resolve this warning. Let me know if someone can help me to come out from this.

Thanks

Nabin Bhandari :

It is just a warning as it will never be null if the response is successful. You can ignore it or wrap around if(response.body() != null) to remove the warning.

Ads ads = response.body();
if(ads != null){
    constant.banner_on = ads.getBanner_on();
    // and so on.
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=432242&siteId=1