0
        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; 
    }
TT.
  • 15,774
  • 6
  • 47
  • 88
Ainsof26
  • 9
  • 3
  • The error message is explicit: `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`. – Slaw Dec 05 '20 at 04:04
  • 2
    In other words, the problem has nothing to do with your `Scanner`; rather, `accessAccount(temp)` is returning `null`. And immediately before the stack trace you've printed "Cannot Find an Account That Matches the ID". So that must mean whatever you read from the scanner is an invalid ID. – Slaw Dec 05 '20 at 04:06
  • 1
    … which is a runtime error and not that the compiler cannot (per the OP's title) 'recognize" something. OP, you're probably a beginner, but it would be useful to note the difference between compiling your program and running it. – user14644949 Dec 05 '20 at 04:11
  • Can you guys please check the accessAccount() method I just added to the original post tell me what makes the problem? – Ainsof26 Dec 05 '20 at 04:17
  • The duplicate should explain what the mistake is in `accessAccount(String)`. In short, don't test for string equality using `==`, use `equals` instead. – Slaw Dec 05 '20 at 04:20
  • if(id.equals(clientId)) in accessAccount method and also check for null before invoking checkBalance() method to avoid NPE – Saad Sahibjan Dec 05 '20 at 04:22
  • Thanks guys! especially Slaw! I just solved it exchanging == with .equals – Ainsof26 Dec 05 '20 at 04:24

1 Answers1

0

As Slaw pointed out, the error is from the checkBalance() method. I believe that the account number passed as parameter to this method is not assoicated to any account and thus, checkBalance() returns null (as no Account object is associated with the account).

This can be due to scan.nextLine() which reads entire line. You can test it by

String s = scan.nextLine();
System.out.println(s.length());

For input as "8102 ", the length will be 8 (4 numbers and 4 spaces). You are better off using scanner.next(); as it reads one word at a time and it does that by using whitespace as a terminator.

String s = scan.next();
System.out.println(s.length());

For "8102 ", the string read by scan.next() will have a length of 4.

Also, the String comparison in the checkBalance() method should be replaced with .equals() method.

Harsh
  • 372
  • 2
  • 15
  • I just tried your suggestion and this is the result temp = scan.nextLine(); System.out.println(temp.length()); CONSOLE : BA0023 6 (the length) To me, it looks like there's no problem with nextLine() itself. – Ainsof26 Dec 05 '20 at 04:20
  • How sure are you that BA0023 is a valid account number? Because the method keeps returning null. Also string checks should be done using `.equals()` method as `==` will check for Object Equality along with Value Equality which is not always true. – Harsh Dec 05 '20 at 04:24
  • You are right! I should've used .equals() to compare the real values of String objects – Ainsof26 Dec 05 '20 at 04:27