将对象添加到 ArrayList 将对象值设置为 null

我有一个改造请求,它从我的 API 请求 GroupAccount 对象列表。API 毫无问题地返回所有值,因此我尝试循环访问我的活动中的响应,它可以完美地检索值,但是一旦我调用


//groupAccounts is my list, declared and initialised globally, groupAccount is the object I create at the end of each iteration

groupAccounts.add(groupAccount);

它将所有 groupAccount 对象值设置为 null。为什么会这样?


GroupAccount 模型类中的构造函数:


public GroupAccount(long groupAccountId,

                  long adminId,

                  String accountName,

                  String accountDescription,

                  int numberOfMembers,

                  BigDecimal totalAmountPaid,

                  BigDecimal totalAmountOwed,

                  int testResourceId) {


}

带有 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;

慕标5832272
浏览 155回答 1
1回答

动漫人物

您尚未在构造函数中设置对象您的代码:public GroupAccount(long groupAccountId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long adminId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String accountName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String accountDescription,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numberOfMembers,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BigDecimal totalAmountPaid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BigDecimal totalAmountOwed,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int testResourceId) {}正确的构造函数:public GroupAccount(long groupAccountId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long adminId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String accountName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String accountDescription,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numberOfMembers,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BigDecimal totalAmountPaid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BigDecimal totalAmountOwed,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java