getResponse()-java.lang.NullPointerException:

时间:2018-09-05 15:50:30

标签: java android nullpointerexception retrofit2

我一直在搜索和搜索如何使它工作,但是我尝试的所有操作都得到了相同的错误。这是我的代码

package com.gwiddle.airsoftcreations.airsoftapp;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import retrofit2.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText; 
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpFragment extends Fragment {

private EditText Name, Username, UserPassword;

private Button BnSignup;


public SignUpFragment() {


}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup 
container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_sign_up, container, 
false);
    Name = view.findViewById(R.id.name);
    Username = view.findViewById(R.id.username);
    UserPassword = view.findViewById(R.id.password);
    BnSignup = view.findViewById(R.id.btnSignup);

    BnSignup.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            performRegistration();
        }
    });

    return view;

}

public void performRegistration(){
    String name = Name.getText().toString();
    String username = Username.getText().toString();
    String password = UserPassword.getText().toString();
    Call<User> call = 
MainActivity.apiInterface.performRegistration(name,username,password);





            call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(@NonNull Call<User> call, @NonNull 
 Response<User> response) {

                    if (response.body().getResponse().equals("ok"))
                    {
                        MainActivity.prefConfig.displayToast("Ready to 
  Deploy.....");
                    }
                    else if (response.body().getResponse().equals("exist"))
                    {
                        MainActivity.prefConfig.displayToast("User Already 
 In Squad, Try again.");

                    }
                    else if (response.body().getResponse().equals("error"))
                    {
                        MainActivity.prefConfig.displayToast("KIA.. 
Something Went Wrong.");

                    }

                }
                }

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

                }
            });

    Name.setText("");
    Username.setText("");
    UserPassword.setText("");
  }
  }

我和我的伴侣都是Java的新手,整天都在尝试解决此问题

这是我得到的错误

  

E / Android运行时:致命异常:主要                     流程:com.gwiddle.airsoftcreations.airsoftapp,PID:20606                     java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String   com.gwiddle.airsoftcreations.airsoftapp.User.getResponse()'为null   对象参考                         在com.gwiddle.airsoftcreations.airsoftapp.SignUpFragment $ 2.onResponse(SignUpFragment.java:64)                         在retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:70)                         在android.os.Handler.handleCallback(Handler.java:809)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:166)                         在android.app.ActivityThread.main(ActivityThread.java:7377)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:469)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

2 个答案:

答案 0 :(得分:0)

在这些检查的一开始就新建一个if,如下所示:

if(response.body() == null || response.body().getResponse() == null ){
//show message or whatever
}

答案 1 :(得分:0)

通读错误,您正面临NullPointerException

  

E / AndroidRuntime:致命异常:主要过程:   com.gwiddle.airsoftcreations.airsoftapp,PID:20606   java.lang.NullPointerException:尝试调用虚拟方法   'java.lang.String   com.gwiddle.airsoftcreations.airsoftapp.User。 getResponse()'   空对象引用在   com.gwiddle.airsoftcreations.airsoftapp.SignUpFragment $ 2。 onResponse (SignUpFragment.java:64)

它表示您已经对null的某些东西调用了getResponse()方法。因此,您必须添加null检查,以确保您不会在null引用上调用方法。

例如,如果字符串可以为null,则在调用其方法之前确保其不为null:

String s = null;
...
if(s != null){
    s.trim();
} 

在代码中使用类似的推理。