I use retrofit to get the response correctly, then I pass the response to an object in the response body, while it fails to get the object in UI thread, there is a NullPointerException Error, I think it's the problem of the asynchronous request, how can I avoid this problem?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
util = new Util(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
util.login("sdjfl@gmail.com", "sdjflsdkf");
// There is a NullPointerException? How Can I
// use this methond correctly in this way?
Log.d("user", util.getUserInfo().toString());
}
});
}
public class Util {
private UserInfo userInfo;
public UserInfo getUserInfo() {
return userInfo;
}
public void login(final String email, final String password) {
Call<UserInfo> loginCall = apiInterface.login(email, password);
loginCall.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
userInfo = response.body();
}
@Override
public void onFailure(Call<UserInfo> call, Throwable t) {
}
});
}