【问题标题】:Adding object to ArrayList sets objects values to null将对象添加到 ArrayList 将对象值设置为 null
【发布时间】:2019-07-15 14:43:39
【问题描述】:

我有一个改造请求,它从我的 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;

          //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) {

  }
});

}

我的列表在课程开始时是这样设置的:

public class Home extends AppCompatActivity {

List<GroupAccount> groupAccounts = new ArrayList<>();
.
...rest of class
.
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    你还没有在构造函数中设置对象

    您的代码:

    public GroupAccount(long groupAccountId,
                      long adminId,
                      String accountName,
                      String accountDescription,
                      int numberOfMembers,
                      BigDecimal totalAmountPaid,
                      BigDecimal totalAmountOwed,
                      int testResourceId) {
    
    }
    

    正确的构造函数:

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

    来源:Java Contrsuctor

    【讨论】:

    • 呃!!对我来说慢了一天。我从主构造函数中删除了一个变量,忘记设置值。感谢您指出!
    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2016-09-26
    • 1970-01-01
    • 2017-11-04
    • 2016-08-10
    相关资源
    最近更新 更多