我正在尝试编写一个简单的银行程序。
基本功能是:
1. open account - (open <amount>)-- returns account number
2.check balance - (balance <AccountNumber>)
下面的代码是这个。我没有发布我的其他类和包含 main() 的类,因为我相信问题出在这段代码中的某个地方::
public class BankAccount {
private static int bankAccNoGen=1001;
private int balance;
private int bankAccNo;
private int i=0;
BankAccount[] bankArray=new BankAccount[10];
public void openBankAccount(int openAmount){
BankAccount ba =new BankAccount();
ba.balance=openAmount;
ba.bankAccNo=bankAccNoGen;
bankArray[i] = ba;
System.out.println("Account opened with the account number : "+bankArray[i].bankAccNo);
System.out.println("Please note the account number for later use.");
System.out.println("Balance for account no : "+bankArray[i].bankAccNo+" is : "+ bankArray[i].balance);
++bankAccNoGen;++i;
}
public void printBalance(int accNo){
for(int i=0;i<10;i++){
if(bankArray[i].bankAccNo==accNo){
System.out.println("Account Number :"+ accNo +" currently has :"+bankArray[i].balance);
}
}
}
}
运行后,我可以通过以下方式开设银行账户:
open 1000
输出:
Account opened with the account number : 1001
Please note the account number for later use.
Balance for account no : 1001 is : 1000
I can check balance by :
balance 1001
错误:
Exception in thread "main" java.lang.NullPointerException at
BankAccount.printBalance(BankAccount.java:27) at
ReadInput.inputRead(ReadInput.java:36) at
SimpleBank.main(SimpleBank.java:11)