json object returning null object reference

Viewed 33

I am using an endpoint that I developed with Java Spring boot to developed an Android app, the response keeps returning null when I tried to register a user. I used the same endpoint in swagger and it's working find but don't know why it's not working in the android studio. I don't know why it keeps returning null. This is the error from the logcat.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.elijah.ukeme.doctorsappointmentapplication, PID: 18696
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.elijah.ukeme.doctorsappointmentapplication.activities.PatientRegistrationActivity$2.onResponse(PatientRegistrationActivity.java:215)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Here is my api localhost:8080/patient/signUp I changed the localhost to my system ip address in the android studio

These are the required parameters

private String name;
    private String email;
    private String password;
    private String gender;
    private String profileImage;
    @JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd")
    private LocalDate dateOfBirth;

My Model Class

public class SignUpPatienceDto {

    @SerializedName("name")
    private String name;
    @SerializedName("email")
    private String email;
    @SerializedName("password")
    private String password;
    @SerializedName("gender")
    private String gender;
    @SerializedName("profileImage")
    private String profileImage;
    @SerializedName("dateOfBirth")
    private LocalDate dateOfBirth;

    public String getGender() {
        return gender;
    }

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



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getProfileImage() {
        return profileImage;
    }

    public void setProfileImage(String profileImage) {
        this.profileImage = profileImage;
    }

    public LocalDate getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(LocalDate dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public SignUpPatienceDto() {
    }
}

Base Url Class

public class BaseUrlClient {

    private static final String BASE_URL = "http://192.168.43.5:8080";

    private static BaseUrlClient mInstance;
    private Retrofit retrofit;

    private BaseUrlClient(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public static synchronized BaseUrlClient getInstance(){
        if (mInstance ==null){
            mInstance = new BaseUrlClient();
        }
        return mInstance;
    }
    public ApiClient getApi(){
        return retrofit.create(ApiClient.class);
    }
}

My ApiClient class

public interface ApiClient {


    @POST("/patient/signUp")
    Call<ResponseBody> createPatient(@Body SignUpPatienceDto signUpPatienceDto);

    @POST("/appointment/book")
    Call<ApiResponse> bookAppointment(@Body AppointmentBookingDto appointmentBookingDto, @Field("token") String token);

}

and here is the activity where I called the method

 @RequiresApi(api = Build.VERSION_CODES.O)
    public void createPatient() {
        loadingPB.setVisibility(View.VISIBLE);
        int selectedID = radioGroup.getCheckedRadioButtonId();
        male = (RadioButton) findViewById(selectedID);
        female = (RadioButton) findViewById(selectedID);
        if (male.isChecked()) {
            selectedGender = male.getText().toString();
        } else if (female.isChecked()) {
            selectedGender = female.getText().toString();
        } else {
            selectedGender = "Not specify";
        }
        date = btnDateOfBirth.getText().toString();
        SignUpPatienceDto signUpPatienceDto = new SignUpPatienceDto();
        signUpPatienceDto.setDateOfBirth(LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd")));
        signUpPatienceDto.setEmail(email.getText().toString());
        signUpPatienceDto.setName(name.getText().toString());
        signUpPatienceDto.setPassword(password.getText().toString());
        signUpPatienceDto.setProfileImage("Profile image not available yet");
        signUpPatienceDto.setGender(selectedGender);

        Call<ResponseBody> call = BaseUrlClient
                .getInstance().getApi().createPatient(signUpPatienceDto);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Toast.makeText(PatientRegistrationActivity.this, response.message(), Toast.LENGTH_SHORT).show();
                Log.d("Main",response.body().toString());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

                Log.d("Main",t.getMessage());
            }
        });

    }

I don't know why it keeps returning null reference when I tried to get the response but the same api endpoint is working fine in both postman and swagger.

1 Answers

Your log says that there is error in this PatientRegistrationActivity on line 215. Here you are trying to convert a null to String. You should put null check before parsing a value. Confirm this using debugger or log

Related