0

I am new to programming, and am working with an Array of objects for the first time.

I have a class called Account, which is to represent a bank account. In the main method of my ATM machine class, I create an array of Account objects. I fill the array with instantiated objects via static method.

I have two additional static methods (one prints out the arrays, which I put in for testing, and another is suppose to check if the Bank Account ID is valid). Both of these static methods throw a Null Pointer Exception when the array of Account objects are passed to these respective static methods.

When I comment these two static methods out, everything works fine. Any help would be appreciated. Code is below:

package banking;

import java.util.Scanner;

public class AtmMachine {
    public static void main(String[] args) {
        //Create account array of size 10
        Account[] bankAccounts = new Account[10];

        //Create 10 accounts
        createAccounts(bankAccounts);

        /*
        //PROBLEM AREA
        //Print Bank Accounts
        printAccounts(bankAccounts);
        */


        //Menu
        menu(bankAccounts);

    }

    public static void createAccounts(Account[] bankAccounts) {
        for (int i = 1; i < bankAccounts.length; i++) {
            bankAccounts[i] = new Account(i, 100);
        }
    }

    public static void menu(Account[] bankAccounts) {
        //Create Scanner object
        Scanner input = new Scanner(System.in);

        while (true) {
            System.out.print("Enter your account number: ");
            int accountNumber = input.nextInt();



            //THIS IS THE PROBLEM
            /*
            //Check to see if a valid account
            if (!isAccount(accountNumber, bankAccounts)) {
                System.out.println("You entered a wrong account number.  Try again.");
                continue;
            }
            */


            //Enter the menu of the account system
            accountMenu(bankAccounts[accountNumber]);


        }
    }

    //THE PROBLEM METHOD
    public static boolean isAccount(int account, Account[] bankAccounts) {
        for (int i = 0; i < bankAccounts.length; i++) {
            if (bankAccounts[i].getId() == account) {
                return true;
            }
        }
        return false;
    }

    public static void accountMenu(Account myAccount) {
        Scanner input = new Scanner(System.in);

        System.out.println("Main Menu: ");
        System.out.println("1. Check Balance");
        System.out.println("2. Withdraw");
        System.out.println("3. Despoit");
        System.out.println("4. Exit");

        int selection = input.nextInt();

        while (selection < 4) {
            if (selection == 1) {
                System.out.printf("The balance is %.2f.\n", myAccount.getBalance());
            } else if (selection == 2) {
                System.out.print("Enter the amount to withdraw: ");
                myAccount.withdraw(input.nextDouble());
                System.out.printf("The new balance is %.2f\n", myAccount.getBalance());
            } else if (selection == 3) {
                System.out.print("Enter the amount to deposit: ");
                myAccount.deposit(input.nextDouble());
            } else {
                System.out.println("You entered a wrong amount.");
                continue;
            }

            System.out.println("Main Menu: ");
            System.out.println("1. Check Balance");
            System.out.println("2. Withdraw");
            System.out.println("3. Despoit");
            System.out.println("4. Exit");

            selection = input.nextInt();


        }

    }

    public static void printAccounts (Account[] bankAccounts){
        for(int i = 0; i < bankAccounts.length; i++){
            System.out.println("Account " + i + " has an id of " + bankAccounts[i].getId() + " and a balance of " + bankAccounts[i].getBalance());
        }
    }

}
DntMesArnd
  • 115
  • 9
  • 2
    You're not initializing the first element of your array, because that loop starts at index 1. – Jorn Vernee Oct 21 '18 at 22:19
  • Thanks Dude, Stupid mistake on my part..... – DntMesArnd Oct 21 '18 at 22:20
  • The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Oct 21 '18 at 22:25

1 Answers1

3

In your createBankAccounts() method you are starting your for loop at 1, not 0. Java arrays are 0-indexed, which means they start at 0. Since your for loop starts at 1, the first element in the array never gets initialized, causing it to throw a NullPointerException.

Change this:

public static void createAccounts(Account[] bankAccounts) {
    for (int i = 1; i < bankAccounts.length; i++) {
        bankAccounts[i] = new Account(i, 100);
    }
}

To this: (int i = 1 becomes int i = 0)

public static void createAccounts(Account[] bankAccounts) {
    for (int i = 0; i < bankAccounts.length; i++) {
        bankAccounts[i] = new Account(i, 100);
    }
}
a.deshpande012
  • 715
  • 7
  • 18