所以,我正在尝试为登录和用户注册应用编写代码。在Snackbar内部,View没有获取任何值并返回nullPoint异常。
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show(); //Line 107
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();
}
});
Logcat出现以下错误。
NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.loginregistration.models.ServerResponse.getMessage()' on a null object reference
at com.learn2crack.loginregistration.RegisterFragment$1.onResponse(RegisterFragment.java:107)
答案 0 :(得分:3)
尝试以下代码
ServerResponse resp = response.body();
if(resp != null && resp.getMessage() != null){
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show(); //Line 107
}
progress.setVisibility(View.INVISIBLE);