本文介绍了方法调用可能会产生 NullPointerException Retrofit Body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Retrofit 2 从我的 API 获取响应并将其值存储在我的常量中,如下所示
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();
}
它向我发出警告,如下所示
It's giving me warning on all three like below
方法调用 getBanner_on 可能会产生 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.
推荐答案
这只是一个警告,因为如果响应成功,它永远不会为空.您可以忽略它或环绕 if(response.body() != null)
以删除警告.
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.
}
这篇关于方法调用可能会产生 NullPointerException Retrofit Body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!