Null-pointer Exception, Retrofit?

对着背影说爱祢 提交于 2019-12-24 23:07:05

问题


I am trying to get an id from URL but getting the null pointer exception

java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.hex.encode.ID_Model$User.id' on a null object reference at com.hex.encode.Home$3.onResponse(Home.java:99)

and this is the code I am using to get the 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//
            }
        });
    } 

and below is the fetchdata class:

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);
    }
}

Edit

Here is the 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;
        }

    }
}

Edit Here is the response from the API Server : https://justpaste.it/4gk8r


回答1:


Your response looks like this

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

Which means you have forgot to parse the graphql key as well as every other key, therefore the user is null.

At an absolute minimum you need a model like this.

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 and generate your model using apollo-codegen (if you use Apollo Client)




回答2:


You need to check response is successful with response.isSucessful(). And it also depends on server side they send the 200 when API successfully hit on server and then we need to check the API response is it success or failure on the basis of the response.

In your case, I think API call is success with 200 and return some response but response.body().getUser() is null. So you have to check the response of API.



来源:https://stackoverflow.com/questions/51519857/null-pointer-exception-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!