-1

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)
Jops
  • 22,535
  • 13
  • 46
  • 63
rahul888
  • 413
  • 2
  • 8
  • 18
  • BankAccount doesnt follows oop! openBankAccount is a bank fn anyway! – internals-in Jul 17 '13 at 16:42
  • Rahul, what do you need the bankArray for? Is it a form of balance history? Coz it's not clear to me why you have an array of bank accounts in a bank account object. – Jops Jul 17 '13 at 17:05

4 Answers4

4

Your bankArray only contains 1 element at [0], so you're getting NullPointer on the second hit, at this point: bankArray[i].balance

enter image description here

Jops
  • 22,535
  • 13
  • 46
  • 63
  • 1
    +1 Where do you get those pics ? Consistently !!! – AllTooSir Jul 17 '13 at 16:51
  • 1
    Thanks for the +1. Not pics, I'm creating them using Gimp. :) – Jops Jul 17 '13 at 16:54
  • 1
    @TheNewIdiot Wow, your reputation is so high now! Great work! . – Jops Jul 17 '13 at 16:55
  • 2
    Aha , that's why you are always the last one to answer :P – AllTooSir Jul 17 '13 at 16:56
  • Yep, while preparing my image, at least 3 would have posted their answers already. :) See you around buddy! – Jops Jul 17 '13 at 16:59
  • Hmm .. may be we can catch on my next trip to Singapore :) – AllTooSir Jul 17 '13 at 16:59
  • @Ronxius Guys I am pretty new to this stuff.. I don't know about array Lists what should I probably do..? – rahul888 Jul 17 '13 at 17:00
  • @rahul888 Try Googling "java tutorial ArrayList". There should be something there that will help. – ajb Jul 17 '13 at 17:06
  • @rahul888 Think of array lists as boxes with numbers where you put your objects, just like my illustration above. Something should be present in the "box" when you access it otherwise, you'll get NullPointerException. Hint: You need to create a loop that will feed all the boxes with values. – Jops Jul 17 '13 at 17:11
  • So I created an array list by: `ArrayList bankArray= new ArrayList();` So how do I add my ba object to this & how do I access the bankAccNo from the arraylist Infact how to print out the ith bankArray element like ( BankArray[i].bankAccNo ) – rahul888 Jul 17 '13 at 17:54
0

You only initialize the first element in the array (bankarray[i] = ba) and when printing you want to access all 10 elements. Fails when accessing bankarray[1] since the array on indexes 1 to 9 is null.

Initilize all elements, then it will work.

Sebastian
  • 5,177
  • 4
  • 30
  • 47
0

Your NullPointerException is from trying to access an index in the bankArray that has not been initialized — instead of a BankAccount, it contains null. Treating null as a BankAccount (by referencing bankArray[i].bankAccNo) throws that exception.

To fix this, you'd need to keep track of which indices have been initialized, or better yet, use an ArrayList.

  • Even better would be to use a [HashMap](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html), since he's trying to use a key (account number) to look for an account. – ajb Jul 17 '13 at 16:49
0

Change this:

if(bankArray[i].bankAccNo==accNo){

to

if (bankArray[i] != null && bankArray[i].bankAccNo == accNo) {
ajb
  • 31,309
  • 3
  • 58
  • 84