I am trying to write a simple Banking program.
The basic functions are :
1. open account - (open <amount>)-- returns account number
2.check balance - (balance <AccountNumber>)
The code for following is this.I am not posting my other classes and the class which contatins main(), because I believe the problem lies somewhere in this code ::
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);
}
}
}
}
After running this I can open a bank account by :
open 1000
output:
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
Error:
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)