0
現在、onResponse()メソッドのgetMessage()の呼び出しによってNullPointerExceptionエラーが発生しています。このonResponse()メソッドは、response.enqueue()コードで必要です。Nullを返すRetroFitレスポンス
06-21 16:12:42.211 28188-28188/com.mtb.login E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mtb.login, PID: 28188
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mtb.login.models.ServerResponse.getMessage()' on a null object reference
at com.mtb.login.LoginFragment$1.onResponse(LoginFragment.java:114)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
これは、エラーが発生している場所です。ここで
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.mtb.login.models.ServerRequest;
import com.mtb.login.models.ServerResponse;
import com.mtb.login.models.User;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class LoginFragment extends Fragment implements View.OnClickListener {
private AppCompatButton btn_login;
private EditText et_email,et_password;
private TextView tv_register,tv_reset_password;
private ProgressBar progress;
private SharedPreferences pref;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login,container,false);
initViews(view);
return view;
}
private void initViews(View view) {
pref = getActivity().getPreferences(0);
btn_login = (AppCompatButton)view.findViewById(R.id.btn_login);
tv_register = (TextView)view.findViewById(R.id.tv_register);
tv_reset_password = (TextView)view.findViewById(R.id.tv_reset_password);
et_email = (EditText)view.findViewById(R.id.et_email);
et_password = (EditText)view.findViewById(R.id.et_password);
progress = (ProgressBar)view.findViewById(R.id.progress);
btn_login.setOnClickListener(this);
tv_register.setOnClickListener(this);
tv_reset_password.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_register:
goToRegister();
break;
case R.id.btn_login:
String email = et_email.getText().toString();
String password = et_password.getText().toString();
if(!email.isEmpty() && !password.isEmpty()) {
progress.setVisibility(View.VISIBLE);
loginProcess(email,password);
} else {
Snackbar.make(getView(), "Fields are empty !", Snackbar.LENGTH_LONG).show();
}
break;
case R.id.tv_reset_password:
goToResetPassword();
break;
}
}
private void loginProcess(String email,String password) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setEmail(email);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.LOGIN_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
if (!response.isSuccessful()) {
ServerResponse resp = response.body();
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN, true);
editor.putString(Constants.EMAIL, resp.getUser().getEmail());
editor.putString(Constants.NAME, resp.getUser().getName());
editor.putString(Constants.UNIQUE_ID, resp.getUser().getUnique_id());
editor.apply();
goToProfile();
}
progress.setVisibility(View.INVISIBLE);
}
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG,"failed");
Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
}
は、私は現在取得していますエラーです:それはここでは
は、次のソースコードであるとき、いくつかの理由について考えました:
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
これは、このNPEを引き起こしているコードの一部である:
resp.getMessage()
なぜこの応答はnullを返しますか?どんな助けでも大歓迎です。