Hi,

I am trying out a bank account program using the corejava package.
I am getting a run-time eception when I create a new account & am unable to trace where the error is.

Plz help. I am giving the code below.


import corejava.Console;/******************************************************************cLASS bANKaCCOUNT REPRESENTING THE RUN-TIME MULTI-USER SYSTEM******************************************************************/public class BankAccount{ /**************************** **DATA OF bANKaCCOUNT CLASS** ****************************/ private static final int MAXACC=10; private static int acc_logged_in; private static int no_acc_created; private static Account[] database; /******************************* **METHODS OF bANKaCCOUNT CLASS** *******************************/ // initialisation block { acc_logged_in=-1; no_acc_created=0; database=new Account[MAXACC]; } private static void createnew() { String str; no_acc_created++; acc_logged_in=no_acc_created; str=Console.readLine("\nEnter your name : "); database[acc_logged_in].SETname(str); database[acc_logged_in].SETbalance(Console.readInt("Enter your initial balance : ")); database[acc_logged_in].SETaccountid((int)(Math.random()*32000)); System.out.println("Your account I.D. is " + database[acc_logged_in].GETaccountid()); System.out.println("Account created successfully...\n\n"); } private static void login() { int i,no; boolean found=false; no=Console.readInt("\nEnter your account I.D. : "); for(i=0;i<no_acc_created;i++) { if(no==database.GETaccountid()) { acc_logged_in=i; found=true; break; } } if(found==true) System.out.println("Logged in successfully as " + database[acc_logged_in].GETname() + "...\n\n"); else System.out.println("Error : The I.D. did not match with any of the accounts !\n\n"); } private static void logoff() { if(acc_logged_in==-1) { System.out.println("\nNo account is currently logged in !\n\n"); } else { acc_logged_in=-1; System.out.println("\nLogged off your account successfully...\n\n"); } } private static int menu() { int choice; System.out.println("\n\t\tBANK-ACCOUNT"); System.out.println("\t\t~~~~~~~~~~~~"); System.out.println(""); System.out.println("\t<1> Create new account"); System.out.println("\t<2> Log in your account"); System.out.println("\t<3> Log off your account"); System.out.println("\t<4> View current status"); System.out.println("\t<5> Deposit an amount"); System.out.println("\t<6> Withdraw an amount"); System.out.println("\t<7> Exit BankAccount.java"); do { choice=Console.readInt("\n\t\tEnter your choice (1 to 7): "); } while(choice<1||choice >7); return choice; } public static void main(String[] args) { int choice; boolean want_to_exit=false; while(want_to_exit==false) { choice=menu(); switch(choice) { case 1: createnew(); break; case 2: login(); break; case 3: logoff(); break; case 4: if(acc_logged_in==-1) { System.out.println("Error : No account is logged in !"); break; } database[acc_logged_in].view(); break; case 5: if(acc_logged_in==-1) { System.out.println("Error : No account is logged in !"); break; } database[acc_logged_in].deposit(); break; case 6: if(acc_logged_in==-1) { System.out.println("Error : No account is logged in !"); break; } database[acc_logged_in].withdraw(); break; case 7: want_to_exit=true; break; } } }}/**********************************************************************cLASS aCCOUNT REPRESENTING EVERY INDIVIDUAL ACCOUNT & ITS DETAILS**********************************************************************/// do not mention public\private when a class does not contain the main() to be executedclass Account{ /************************ **DATA OF aCCOUNT CLASS** ************************/ private String name; private int accountid; private int balance; /*************************** **METHODS OF aCCOUNT CLASS** ***************************/ public Account() { name=""; accountid=-1; balance=0; } public int GETaccountid() { return accountid; } public String GETname() { return name; } public void SETname(String str) { name=str; } public void SETaccountid(int id) { accountid=id; } public void SETbalance(int bal) { balance=bal; } public void view() { System.out.println("\nName : " + name); System.out.println("Balance : " + balance + " Rs.\n\n"); } public void deposit() { int dep=Console.readInt("\nEnter the amount you want to deposit : "); balance+=dep; System.out.println("Amount deposited successfully...\n\n"); } public void withdraw() { int wit=Console.readInt("\nEnter the amount you want to withdraw : "); if(wit<=balance) { balance-=wit; System.out.println("Amount withdrawn successfully...\n\n"); } else { System.out.println("Error : The amount you are attempting to"); System.out.println(" withdraw is greater than your balance !\n\n"); } }}

The exception is encountered after I enter a name.

Exception in thread "main" java.lang.NullPointerException

I tried out initialising each account object but even that doesn't work.

Plz help.

oh no..i forgot to put the code tags..

here is my improved program..the thing in bold is the suggested correction.

import corejava.Console;
/****************************************************************
**cLASS bANKaCCOUNT REPRESENTING THE RUN-TIME MULTI-USER SYSTEM**
****************************************************************/
public class BankAccount
{
    /****************************
    **DATA OF bANKaCCOUNT CLASS**
    ****************************/
    private static final int MAXACC=10;
    private static int acc_logged_in;
    private static int no_acc_created;
    private static Account[] database;
    /*******************************
    **METHODS OF bANKaCCOUNT CLASS**
    *******************************/
    // initialisation block
    {
        acc_logged_in=-1;
        no_acc_created=0;
        database=new Account[MAXACC];
        [B]for(int i=0;i<MAXACC;i++)
            database[i]=new Account();
[/B]    }
    private static void createnew()
    {
        String str;
        no_acc_created++;
        acc_logged_in=no_acc_created;
        str=Console.readLine("\nEnter your name : ");
        database[acc_logged_in].SETname(str);
        database[acc_logged_in].SETbalance(Console.readInt("Enter your initial balance : "));          
        database[acc_logged_in].SETaccountid((int)(Math.random()*32000));                              
        System.out.println("Your account I.D. is " + database[acc_logged_in].GETaccountid());
        System.out.println("Account created successfully...\n\n");
    }
    private static void login()
    {
        int i,no;
        boolean found=false;
        no=Console.readInt("\nEnter your account I.D. : ");
        for(i=0;i<no_acc_created;i++)
        {
            if(no==database[i].GETaccountid())
            {
                acc_logged_in=i;
                found=true;
                break;
            }
        }
        if(found==true)
            System.out.println("Logged in successfully as " + database[acc_logged_in].GETname() + "...\n\n");
        else
            System.out.println("Error : The I.D. did not match with any of the accounts !\n\n");
    }
    private static void logoff()
    {
        if(acc_logged_in==-1)
        {
            System.out.println("\nNo account is currently logged in !\n\n");
        }
        else
        {
            acc_logged_in=-1;
            System.out.println("\nLogged off your account successfully...\n\n");
        }
    }
    private static int menu()
    {
        int choice;
        System.out.println("\n\t\tBANK-ACCOUNT");
        System.out.println("\t\t~~~~~~~~~~~~");
        System.out.println("");
        System.out.println("\t<1> Create new account");
        System.out.println("\t<2> Log in your account");
        System.out.println("\t<3> Log off your account");
        System.out.println("\t<4> View current status");
        System.out.println("\t<5> Deposit an amount");
        System.out.println("\t<6> Withdraw an amount");
        System.out.println("\t<7> Exit BankAccount.java");
        do
        {
            choice=Console.readInt("\n\t\tEnter your choice (1 to 7): ");
        }
            while(choice<1||choice >7);
        return choice;
    }
    public static void main(String[] args)
    {
        int choice;
        boolean want_to_exit=false;
        while(want_to_exit==false)
        {
            choice=menu();
            switch(choice)
            {
                case 1:
                    createnew();
                    break;
    
                case 2:
                    login();
                    break;
    
                case 3:
                    logoff();
                    break;
    
                case 4:
                    if(acc_logged_in==-1)
                    {
                        System.out.println("Error : No account is logged in !");
                        break;
                    }
                    database[acc_logged_in].view();
                    break;
    
                case 5:
                    if(acc_logged_in==-1)
                    {
                        System.out.println("Error : No account is logged in !");
                        break;
                    }
                    database[acc_logged_in].deposit();                
                    break;
    
                case 6:
                    if(acc_logged_in==-1)
                    {
                        System.out.println("Error : No account is logged in !");
                        break;
                    }
                    database[acc_logged_in].withdraw();                 
                    break;
    
                case 7:
                    want_to_exit=true;
                    break;
            }
        }
    }
}
/********************************************************************
**cLASS aCCOUNT REPRESENTING EVERY INDIVIDUAL ACCOUNT & ITS DETAILS**
********************************************************************/
// do not mention public\private when a class does not contain the main() to be executed
class Account
{
    /************************
    **DATA OF aCCOUNT CLASS**
    ************************/
    private String name;
    private int accountid;
    private int balance;
    /***************************
    **METHODS OF aCCOUNT CLASS**
    ***************************/ 
    public Account()
    {
        name="";
        accountid=-1;
        balance=0;
    }
    public int GETaccountid()
    {
        return accountid;
    }
    public String GETname()
    {
        return name;
    }
    public void SETname(String str)
    {
        name=str;
    }
    public void SETaccountid(int id)
    {
        accountid=id;
    }
    public void SETbalance(int bal)
    {
        balance=bal;
    }
    public void view()
    {
        System.out.println("\nName : " + name);
        System.out.println("Balance : " + balance + " Rs.\n\n");
    }
    public void deposit()
    {
        int dep=Console.readInt("\nEnter the amount you want to deposit : ");
        balance+=dep;
        System.out.println("Amount deposited successfully...\n\n");
    }
    public void withdraw()
    {
        int wit=Console.readInt("\nEnter the amount you want to withdraw : ");
        if(wit<=balance)
        {
            balance-=wit;
            System.out.println("Amount withdrawn successfully...\n\n");
        }
        else
        {
            System.out.println("Error : The amount you are attempting to");
            System.out.println(" withdraw is greater than your balance !\n\n");
        }
    }
}

Actually, the part in bold was not necessarily what I had in mind - but it does fix the error. You really don't need to create Account objects until they are requested, as in createNew(). You would need to take care to make null checks before performing any operations on an array reference of course.

It would actually be easier if you used an ArrayList or Vector instead of the array for accounts. You would not be limited to a certain number and would not need to worry about null elements in the array. Optionally, you could also use a HashMap to store the account objects and use the account number as the key. This would eliminate having to loop all elements to find the account in login().

Ok, I missed one other thing on my first glance on the code. Your initialization block is not getting called because it is not static and you are using the class in a static manner. Add the 'static' keyword before your initialization block and it will work fine:

// initialisation block
    static {
        acc_logged_in=-1;
        no_acc_created=0;
        database=new Account[MAXACC];
        [B]for(int i=0;i<MAXACC;i++)
            database[i]=new Account();
[/B]    }

The initialization would have been called if you had created an instance of BankAccount, but in a static context you must also prefix such initialization blocks with the static keyowrd.

This question has already been answered. Start a new discussion instead.