gpt4 book ai didi

java - 由于某种原因,我的客户对象为空。

转载 作者:行者123 更新时间:2023-12-01 15:07:53 26 4
gpt4 key购买 nike

我有一个 foreach 循环,它迭代每个首选客户并总结他们的余额以创建他们的总 Assets 。然而,每次它查看客户对象时,都会将其评估为空。 PreferredCustomer 是抽象类 Customer 的子类。我假设我没有正确执行 foreach 循环,但我不确定我做错了什么。

 public static void getBalance() {
for(Customer customer: preferredCustomers){
ArrayList<Account> al = customer.getAccountList();
for(Account account: al){
totalAssets+=account.getBalance();
//balance = account.getbalance();
}
customer.setBalance(totalAssets);
}
}

此外,Arraylist AccountList 是每个客户的帐户对象列表。

最佳答案

将来,如果您的代码抛出 NPE,您应该在帖子中包含异常,并指向与示例代码相对应的行:

  • 如果您在 for(Customer 上抛出 NPE循环,然后是你的preferredCustomers字段是 null .
  • 如果它在 customer.getAccountList() 上抛出 NPE行,然后是 preferredCustomers 中的元素之一 Collection 是null .
  • 如果它在 for(Account account 上抛出 NPE循环然后你的 preferredCustomers 之一有一个帐户列表是 null .
  • 如果它在 totalAssets+=account.getBalance(); 上抛出 NPE行,则列表中的帐户之一是 null 。余额字段也可能是 null Long对象。

正如@ColinD提到的,学习how to use a debugger in Eclipse推荐。

关于java - 由于某种原因,我的客户对象为空。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12735243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com