0

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
.
}

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Duh!! Slow day for me.. I removed one variable from my main constructor and forgot to set the values. Thanks for pointing it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.