Ad
Android MVVM And Retrofit Api Response Is Null
This function is used to Login the user. I am using data binding and RxJava
public void loginUser(String idNumber, String password) {
RetrofitService.getApiService()
.agentLogin(new LoginRequest(idNumber, password))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Response<LoginResponse>>() {
@Override
public void call(Response<LoginResponse> userResponse) {
if (userResponse.isSuccessful() ) {
executor.execute(new Runnable() {
@Override
public void run() {
Log.i("AUTHKEY",userResponse.body().getUser().getAuthKey());
userDao.insert(userResponse.body().getUser());
}
});
}
}
});
}
The app fails when I log Log.i("AUTHKEY",userResponse.body().getUser().getAuthKey());
Error java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Integer com.vuna.agent.model.LoginResponse$User.getId()' on a null object reference
From the Okhttp logger, the server returns the right response
2019-04-12 12:33:36.707 28896-29064/com.vuna.agent D/OkHttp: <-- 200 OK http://example.com/login (923ms)
2019-04-12 12:33:36.709 28896-29064/com.vuna.agent D/OkHttp: {"code":201,"message":"You are now logged in!","id":2,"firstname":"Victor","lastname":"Amwollo","authkey":"22sA1JvVo4hmzhps1NVIO7_B6f4yCp_e"}
2019-04-12 12:33:36.709 28896-29064/com.vuna.agent D/OkHttp: <-- END HTTP (141-byte body)
Here is the LoginResponse class with an inner Entity class
public class LoginResponse {
private Boolean success;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Entity(tableName = "user")
public static class User {
@PrimaryKey(autoGenerate = true)
@SerializedName("id")
private Integer id;
private String firstName;
private String lastName;
private String authKey;
//getters and setters
}
}
Why am I getting the error?
Ad
Answer
You get the null pointer because your json does not map to LoginResponse.
try
public class LoginResponse {
private Boolean success;
private Integer id;
private String firstName;
private String lastName;
private String authKey;
}
I cleaned up your RX. Note: Brackets might be wrong as I am used to kotlin.
public void loginUser(String idNumber, String password) {
RetrofitService.getApiService()
.agentLogin(new LoginRequest(idNumber, password))
.filter { it.isSuccessful(); }
.map { it.body(); }
.map { new User(it.getId()...) /* covert LoginResponse to User here */ }
.flatMapCompletable { Completable.fromAction { userDao.insert(it); } }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad