Retrofit error: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference

▼魔方 西西 提交于 2021-01-29 10:13:51

问题


I am trying to create an app to allow users to register and account.

I have seen this question asked a few times but i wasn't able to fix mine using the same solutions

I am getting the error

/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 12181
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.ResponseBody.string()' on a null object reference
        at com.example.app.MainActivity$1.onResponse(MainActivity.java:75)
        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)
I/Process: Sending signal. PID: 12181 SIG: 9
Process 12181 terminated.

My Retrofit Client is this

public class RetrofitClient {

    private static final String BASE_URL = "http://123.456.789.101:5000/api/";
    private static RetrofitClient mInstance;
    private Retrofit retrofit;

    private RetrofitClient(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static synchronized RetrofitClient getInstance() {

        if (mInstance == null) {
            mInstance = new RetrofitClient();
        }


        return mInstance;

    }

    public Api getApi(){
        return retrofit.create(Api.class);
    }
}

The API interface is this

public interface Api {
    @FormUrlEncoded
    @POST("/users/")
    Call<ResponseBody> createUser(
            @Field("email") String email,
            @Field("password") String password

    );
}

And the Main activity is this

Call<ResponseBody> call = RetrofitClient
                .getInstance()
                .getApi()
                .createUser(email, password);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String s = response.body().string();
                    Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();

            }
        });

The response is null so i am assuming it is a problem with the RetrofitClient but unsure how to fix


回答1:


maybe your response is null, first check it.

if(response.body() != null) {
        String s = response.body().string();
    }


来源:https://stackoverflow.com/questions/60404776/retrofit-error-java-lang-nullpointerexception-attempt-to-invoke-virtual-method

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