Scanner scan = new Scanner(System.in);
AccountList accounts = new AccountList();
accounts.addAccount(new BankAccount(100000, "Mark", "BA0021"));
accounts.addAccount(new BankAccount(367000, "John", "BA0022"));
accounts.addAccount(new BankAccount(94500, "Michael", "BA0023"));
accounts.accessAccount("BA0021").checkBalance();
String temp = "BA0022";
accounts.accessAccount(temp).checkBalance();
**temp = scan.nextLine();
accounts.accessAccount(temp).checkBalance()**;
As you can see, I tried :
**temp = scan.nextLine();
accounts.accessAccount(temp).checkBalance();**
like the other two methods I tried and succeeded :
accounts.accessAccount("BA0021").checkBalance();
String temp = "BA0022";
accounts.accessAccount(temp).checkBalance();
And the console shows :
Mark's balance is $100000
John's balance is $367000
**BA0023
Cannot Find an Account That Matches the ID**
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "ainsof26.project.banking.BankAccount.checkBalance()" because the return value of "ainsof26.project.banking.AccountList.accessAccount(String)" is null
at BankingApplication/ainsof26.project.banking.BankingApplication.main(BankingApplication.java:22)
Can you recognize what the problem here is?
This is accessAccount method :
BankAccount accessAccount(String id) {
for(BankAccount account: accountList) {
if(account.clientId == id) {
return account;
}
}
System.out.println("Cannot Find an Account That Matches the ID");
return null;
}