【问题标题】:Method invocation may produce NullPointerException Retrofit Body方法调用可能产生 NullPointerException Retrofit Body
【发布时间】:2020-09-26 17:56:03
【问题描述】:

我正在使用 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();
                }

它在下面的所有三个方面都给了我警告

方法调用getBanner_on可能产生java.lang.nullPointerException

我很困惑,无法解决此警告。让我知道是否有人可以帮助我摆脱困境。

【问题讨论】:

  • 这是一个警告,因为响应可能为空,所以为了安全起见,Android Studio 会发出警告,如果没有收到异常,您可以断言 response!=null 并继续。
  • 是的,这可能是响应可能为空,因此将代码包装到 if 语句中,最好通过 NullPointerExecption 保存您的应用程序,就像您之前的问题一样
  • 这能回答你的问题吗? Why should one use Objects.requireNonNull()?

标签: java android retrofit retrofit2


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    如果您在分配前使用Java 8, you can do a assertion

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

    另一种方法是在分配前使用 Objects.requireNonNull()

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

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

    /**
         * 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>
         * 
    

    一个很棒的SO answer about this is here。还有read this to get understanding why we need to explicitly check.

    【讨论】:

      【解决方案3】:

      只需使用此null 指针检查即可。

      If(response != null && response.isSuccessfull())
      {
      
      // body
      
      }
      

      【讨论】:

      • @priya 我不确定这一点,但你能检查一下 response.getBody() 的空指针吗?也就是说 if (response !=null && respone.getBody() !=Null) { ... }
      【解决方案4】:

      在从响应句柄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();
           }
         }
      

      【讨论】:

        猜你喜欢
        • 2020-11-05
        • 2015-02-22
        • 2019-12-06
        • 1970-01-01
        • 2016-10-25
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多