Java 方法调用可能会产生 NullPointerException Retrofit Body

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46519388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:24:09  来源:igfitidea点击:

Method invocation may produce NullPointerException Retrofit Body

javaandroidretrofitretrofit2

提问by Priya

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

我正在使用 Retrofit 2 从我的 API 获取响应并将其值存储在我的常量中,如下所示

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

谢谢

采纳答案by 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.

这只是一个警告,因为如果响应成功,它永远不会为空。您可以忽略它或环绕if(response.body() != null)以删除警告。

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

回答by emilpmp

Just use this nullpointer check.

只需使用此null指针检查。

If(response != null && response.isSuccessfull())
{

// body

}

回答by Upendra Shah

Its a good habit to before assigning any value from response handle null pointer exceptionto prevent some of exception. And you can also use tryand catchto handle other exceptions.

在从响应句柄分配任何值之前,它是一个好习惯,null pointer exception以防止一些异常。您还可以使用trycatch来处理其他异常。

 if(response.isSuccessful()) {
    try {
         if(response.body() != null){
                constant.banner_on = response.body().getBanner_on();
                constant.int_on = response.body().getInt_on();
                constant.int_click = response.body().getInt_click();
           }
     } catch (IOException e) {
        e.printStackTrace();
     }
   }

回答by Blasanka

Using ifis great but there is only one line, much cleaner way is:

使用if很棒,但只有一行,更简洁的方法是:

constant.banner_on = ads != null ? ads.getBanner_on() : null;

If you are using Java 8, you can do a assertionbefore assignment:

如果您使用的是Java 8,则可以在赋值之前进行断言

Ads ads = response.body();
assert ads != null;
constant.banner_on = ads.getBanner_on();

Another way to do this is using Objects.requireNonNull()before assignment:

另一种方法是Objects.requireNonNull()在赋值之前使用:

constant.banner_on = Objects.requireNonNull(ads.getBanner_on());

That is actually designed primarily for param validation. Source code comment:

这实际上主要是为参数验证而设计的。源代码注释:

/**
     * Checks that the specified object reference is not {@code null}. This
     * method is designed primarily for doing parameter validation in methods
     * and constructors, as demonstrated below:
     * <blockquote><pre>
     * public Foo(Bar bar) {
     *     this.bar = Objects.requireNonNull(bar);
     * }
     * </pre></blockquote>
     * 

One great SO answer about this is here. Also read this to get understanding why we need to explicitly check..

关于这一点的一个很好的SO 答案就在这里。另请阅读本文以了解为什么我们需要明确检查。.