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());
}
}
}