Reputation: 454
I have a Retrofit request which requests a list of GroupAccount objects from my API. The API returns all values without a problem, so I then try to loop over the response in my activity, which retrieves the values perfectly, but once I call
//groupAccounts is my list, declared and initialised globally, groupAccount is the object I create at the end of each iteration
groupAccounts.add(groupAccount);
It sets all of the groupAccount objects values to null. Why is this happening?
Constructor in GroupAccount model class:
public GroupAccount(long groupAccountId,
long adminId,
String accountName,
String accountDescription,
int numberOfMembers,
BigDecimal totalAmountPaid,
BigDecimal totalAmountOwed,
int testResourceId) {
}
The request method with onResponse & onFailure:
public void getUserAssociatedAccounts(String userId){
Call<List<GroupAccount>> call = apiInterface.getUserAssociatedAccounts(userId);
call.enqueue(new Callback<List<GroupAccount>>() {
@Override
public void onResponse(Call<List<GroupAccount>> call, Response<List<GroupAccount>> response) {
if(!response.isSuccessful()) {
//Handle
} else {
if(response.body().size() > 0){
for(int i=0; i<response.body().size(); i++) {
//This is just for clarity
long groupAccountId = response.body().get(i).getGroupAccountId();
long adminId = response.body().get(i).getAdminId();
String accountName = response.body().get(i).getAccountName();
String accountDescription = response.body().get(i).getAccountDescription();
int numberOfMembers = response.body().get(i).getNumberOfMembers();
BigDecimal amountPaid = response.body().get(i).getTotalAmountPaid();
BigDecimal amountOwed = response.body().get(i).getTotalAmountOwed();
int testRes = R.drawable.human_photo;
//Value setting works fine
GroupAccount groupAccount = new GroupAccount(groupAccountId,
adminId,
accountName,
accountDescription,
numberOfMembers,
amountPaid,
amountOwed,
testRes);
//Values are now null here???
groupAccounts.add(groupAccount);
}
setUpFAB();
setUpNavDrawer();
setUpAccountPreviewRecyclerView();
emptyRVTextViewSetUp(checkIfListIsEmpty(groupAccounts));
}
}
}
@Override
public void onFailure(Call<List<GroupAccount>> call, Throwable t) {
}
});
}
My list is set like this at the start of the class:
public class Home extends AppCompatActivity {
List<GroupAccount> groupAccounts = new ArrayList<>();
.
...rest of class
.
}
Upvotes: 0
Views: 167
Reputation: 854
You have not set the objects in the constructor
Your Code:
public GroupAccount(long groupAccountId,
long adminId,
String accountName,
String accountDescription,
int numberOfMembers,
BigDecimal totalAmountPaid,
BigDecimal totalAmountOwed,
int testResourceId) {
}
Correct Constructor:
public GroupAccount(long groupAccountId,
long adminId,
String accountName,
String accountDescription,
int numberOfMembers,
BigDecimal totalAmountPaid,
BigDecimal totalAmountOwed,
int testResourceId) {
this.groupAccountId = groupAccountId;
this.adminId = adminId;
this.accountName = accountName;
this.accountDescription = accountDescription;
this.numberOfMembers = numberOfMembers;
this.totalAmountPaid = totalAmountPaid;
this.totalAmountOwed = totalAmountOwed;
this.testResourceId = testResourceId;
}
Source: Java Contrsuctor
Upvotes: 1