【问题标题】:Null-pointer Exception, Retrofit?空指针异常,改造?
【发布时间】:2018-08-21 13:57:12
【问题描述】:

我正在尝试从 URL 获取 id,但得到空指针异常

java.lang.NullPointerException:尝试从空对象引用上的字段“java.lang.String com.hex.encode.ID_Model$User.id”读取 在 com.hex.encode.Home$3.onResponse(Home.java:99)

这是我用来获取 id 的代码:

private void GetID() {
        Call<ID_Model> idcall = fetchdata.getIdService().getID(user_search);
        idcall.enqueue(new Callback<ID_Model>() {
            @Override
            public void onResponse(@NonNull Call<ID_Model> call,@NonNull Response<ID_Model> response) {
                assert response.body() != null;
                String fetched_id =response.body().getUser().getId;//this is where the exception is poping
                GetData(fetched_id);
            }

            @Override
            public void onFailure(@NonNull Call<ID_Model> call, @NonNull Throwable t) {
                //something//
            }
        });
    } 

下面是fetchdata类:

class fetchdata {
    private static UserFetchData userdatafetch = null;
    private static UserIDFetch fetchService = null;

    static  UserIDFetch getIdService() {
        if (fetchService == null) {
            String id_url = "url_and_it's_working";
            Retrofit fetch_id = new Retrofit.Builder()
                    .baseUrl(id_url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            fetchService = fetch_id.create(UserIDFetch.class);
        }
        return fetchService;
    }

    public interface UserIDFetch{
        @GET("{user_input}"+"/")
        Call<ID_Model> getID(@Path("user_input") String s);
    }
}

编辑

这是 Id_Model :

public class ID_Model {

    @SerializedName("user")
    @Expose
    public User user;

    public User getUser() {
        return user;
    }

    public class User {

        @SerializedName("id")
        @Expose
        public String id;

        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }

    }
}

编辑 这是来自 API 服务器的响应: https://justpaste.it/4gk8r

【问题讨论】:

  • 也许使用 response.isSucessful()
  • ID_Model 如果改造未能将 html 正文解析到您的对象中,则将为 null。请显示原始 url 响应和您的模型类
  • hii @cricket_007 我已经在主要问题中发布了 ID_MODEL 类,请看一下。
  • @WeslleyBarbosa 检查我使用了 OnResponce 回调,因此响应成功,ID_Model 中有内容。我有更新问题。
  • 总是收到响应,但是响应码可以是 200(OK)、400(BAD REQUEST)

标签: android nullpointerexception retrofit2


【解决方案1】:

您的回复如下所示

{
   ... 
  "graphql": {
     "user": {
       ... 
      "id": "1067259270",

这意味着您忘记解析graphql 键以及其他所有键,因此用户为空。

至少您需要这样的模型。

class ResponseModel {
    GraphQLModel graphql;

    class GraphQLModel {
        UserModel user;

        class UserModel {
            int id;
        } 
    } 
} 

My suggestion would be to use an actual GraphQL library rather than Retrofit 并使用 apollo-codegen 生成您的模型(如果您使用 Apollo 客户端)

【讨论】:

  • 是的,刚刚发现,我该如何更新当前的 ID_MODEL 呢?
  • 我会使用 apollo-codegen 项目为您创建每个 GraphQL 模型,而不是手动更改模型
【解决方案2】:

您需要使用response.isSucessful() 检查响应是否成功。它还取决于服务器端当 API 成功命中服务器时他们发送200 然后我们需要检查 API 响应是success 还是@987654324 @ 基于响应。

在你的情况下,我认为 API 调用是成功的 200 并返回一些响应,但 response.body().getUser()null。所以你必须检查 API 的响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多