0

I am trying to implement Retrofit in my app. getting below error :

 java.lang.NullPointerException: 
Attempt to invoke virtual method 'java.lang.String com.retrofitd.rest.LoginModel$Data.getFirstName()' on a null object reference
                                                                 at com.retrofitd.MainActivity$1$1.onResponse(MainActivity.java:46)
                                                                 at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                 at android.os.Handler.handleCallback(Handler.java:742)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5527)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

My API CLIENT class is as below :

public class ApiClient {

    public static final String BASE_URL = "http://999.99.999.99/~test/master/api/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

API INTERFACE is as below :

public interface ApiInterface {//    gym/gymdetail
    @POST("webservice/login")
    Call<LoginModel> getGymDetails(@Query("username") String userId,@Query("password")String strGymId);
}

and below MainActivity.java, using like this :

ApiInterface apiService =
                    ApiClient.getClient().create(ApiInterface.class);

            Call<LoginModel> call = apiService.getGymDetails("sssss", "123456");
            call.enqueue(new Callback<LoginModel>() {
                @Override
                public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {

                    Toast.makeText(MainActivity.this, "" + response.body().getData().getFirstName(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<LoginModel> call, Throwable t) {
                    Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
                }
            });
        }

Also set Internet permission in manifest. What might be the issue ? Thanks.

EDIT Let me add my model class. My model class for login is as below. Please, check it out. my model class :

public class LoginModel {

@SerializedName("status")
@Expose
private Integer status;
@SerializedName("data")
@Expose
private Data data;
@SerializedName("message")
@Expose
private String message;

public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public class Data {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("first_name")
    @Expose
    private String first_name;
    @SerializedName("last_name")
    @Expose
    private String last_name;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("password")
    @Expose
    private String password;
    @SerializedName("photo")
    @Expose
    private String photo;
    @SerializedName("birthdate")
    @Expose
    private String birthdate;
    @SerializedName("gender")
    @Expose
    private String gender;

    public String getId() {
        return id;
    }

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

    public String getFirstName() {
        return first_name;
    }

    public void setFirstName(String first_name) {
        this.first_name = first_name;
    }

    public String getLastName() {
        return last_name;
    }

    public void setLastName(String last_name) {
        this.last_name = last_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

}

jaimin
  • 23
  • 1
  • 8

3 Answers3

0

use Postman to check the response first, if it works properly then you have build POJO based on JSON rawdata using JSON schema to POJO. while sending request you have to pass that POJO class in it your problem is going to solve.

Anbuselvan Rocky
  • 606
  • 6
  • 22
vinay
  • 149
  • 7
  • { "status": 1, "data": { "id": "551", "first_name": "Ssss", "last_name": "Sdsd", "email": "jaiminmoditest@jdmail.com", "username": "sssss", "password": "e10adc3949ba59abbe56e057f20f883e", "photo": "", "birthdate": "1963-02-13", "gender": "" }, "message": "Successful Login" } – jaimin Mar 22 '18 at 11:00
  • then use this raw jsondata to build pojo class using json schema to pojo website rest u can find answers anywhere – vinay Mar 22 '18 at 11:02
  • in response do you getting first name in jsondata – vinay Mar 22 '18 at 11:04
  • since I was posting I changed my lines : @POST("webservice/login") @FormUrlEncoded Call getGymDetails(@Field("username") String userId, @Field("password")String strGymId); But, getting Failure. – jaimin Mar 22 '18 at 11:07
  • OHH MY GOD, the issue was : NOTHING. Actually, My mobile data was turned off. just on it. and getting success instead of onFailure() method called. Sry for not checking internet connection before calling api. ;) – jaimin Mar 22 '18 at 11:30
  • for that first of all you have to check for internet connection programatically if it was not connected you have to show any toast message – vinay Mar 22 '18 at 11:35
  • ya Vinay, i know. :) – jaimin Mar 22 '18 at 12:06
0

create a jsonobject like this and post it in body

JSONObject jsonObject=new JSONObject();
    try {
        jsonObject.put("username","Ssss");
        jsonObject.put("password","Sdsd");
    } catch (JSONException e) {
        e.printStackTrace();
    }

and change api interface call like this

Call<LoginModel> call = apiService.getGymDetails(jsonObject);

change interface like this

public interface ApiInterface {
@POST("webservice/login")
@Headers("Content-Type: application/json")
Call<LoginModel> getGymDetails(@Body JSONObject jsonObject);
}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
0

there can be many reasons for that .

1) your model class that you defined is is not matching the json coming from request

2) you are using wrong method eg post or get ?

but i see there can be a problem in your model class post it here so that it can be looked on to ?

hamza khan
  • 141
  • 1
  • 11
  • Posted my model. – jaimin Mar 22 '18 at 11:23
  • OHH MY GOD, the issue was : NOTHING. Actually, My mobile data was turned off. just on it. and getting success instead of onFailure() method called. Sry for not checking internet connection before calling api. ;) – jaimin Mar 22 '18 at 11:31