Hey all. I'm having a little problem with my code. The program is supposed to keep track of different customers banking accounts in an arraylist, and have the ability to add a new customer and account, search for any account, make a deposit to any account, make a withdrawal from any account, and print out all accounts. There seems to be a problem with the addAccount method, because whenever I try to add an account it gives me the following error:
Exception in thread "main" java.lang.NullPointerException
at customer.Bank.search(Bank.java:22)
at customer.Bank.addAccount(Bank.java:34)
at customer.BankTest.choiceCheck(BankTest.java:56)
at customer.BankTest.main(BankTest.java:27)
It seems to be based in the search method in the Bank class, as thats where the error is pointing me too. Can anyone give me a clue as to what I am doing wrong. I've commented next to line of code where the problem starts.
import java.util.*; public class BankTest { public static void main(String[] args){ Scanner keyboard = new Scanner(System.in); String input; do{ System.out.println("Welcome to Fictional Bank. \nPlease choose" + "from one of the options below. \n"); System.out.println("Type 'a' to add a new customer and account. " + "\nType 'd' to make a deposit to an account. " + "\nType 'w' to make a withdrawl from an account. " + "\nType 'f' to find an account." + "\nType 'p' to print all of the accounts." + "\nType 'q' to quit."); input = keyboard.nextLine(); choiceCheck(input, keyboard); }while(!input.equals("q")); } public static void choiceCheck(String input, Scanner keyboard){ String name; String address; int age; String telephoneNumber; double balance; if (input.equals("a")) { System.out.println("What is the customer's name?"); name = keyboard.nextLine(); System.out.println("What is the customer's address?"); address = keyboard.nextLine(); System.out.println("What is the customer's age?"); age = keyboard.nextInt(); System.out.println("What is the customer's telephone number?"); telephoneNumber = keyboard.next(); Customer newCust = new Customer(name, address, age, telephoneNumber); System.out.println("How much did you want to add to the " + "account"); balance = keyboard.nextDouble(); Bank.addAccount(newCust, balance, keyboard); } else if (input.equals("d")){ System.out.println("What is the customer's name?"); name = keyboard.nextLine(); Customer tempCust = new Customer(name, null, 0, null); Bank.makeDeposit(tempCust, keyboard); } else if (input.equals("w")){ System.out.println("What is the customer's name?"); name = keyboard.nextLine(); Customer tempCust = new Customer(name, null, 0, null); Bank.makeWithdrawl(tempCust, keyboard); } else if (input.equals("f")){ System.out.println("What is the customer's name?"); name = keyboard.nextLine(); Customer tempCust = new Customer(name, null, 0, null); Bank.getAccount(tempCust, keyboard); } } }
import java.util.*; public class Bank { private static ArrayList<Account> accountList; public Bank(){ accountList = new ArrayList<>(); } private static int search(Customer c){ int i; Customer tempCust = new Customer(c.getName(), null, 0, null); for (i = 0; i < accountList.size(); i++){ //****This is the problem code****// if (accountList.get(i).equals(tempCust)) { return i; } } return -1; } public static boolean addAccount(Customer c, double b, Scanner keyboard){ System.out.println("What kind of account would you like to open? " + "\n Type 'c' for checking and 's' for saving."); String input = keyboard.nextLine(); int index = search(c); if (input.equals("c")){ if (index == -1){ accountList.add(new CheckingAccount(c, b)); return true; } else { return false; } } if (input.equals("s")){ if (index == -1){ accountList.add(new SavingAccount(c, b)); return true; } else { return false; } } return false; } public static boolean makeDeposit(Customer c, Scanner keyboard){ int index = search(c); if (index == -1){ System.out.println("Customer does not exist in the system."); return false; } else{ System.out.println("How much would you like to deposit?"); double depositAmount = keyboard.nextDouble(); double newBalance = (accountList.get(index).getBalance()) + depositAmount; accountList.get(index).setBalance(newBalance); System.out.println("The deposit was successful. Your new balance " + "is $" + newBalance); return true; } } public static boolean makeWithdrawl(Customer c, Scanner keyboard){ int index = search(c); if (index == -1){ System.out.println("Customer does not exist in the system."); return false; } else{ System.out.println("How much would you like to withdraw?"); double withdrawalAmount = keyboard.nextDouble(); double newBalance = (accountList.get(index).getBalance()) - withdrawalAmount; accountList.get(index).setBalance(newBalance); System.out.println("The withdraw was successful. Your new balance " + "is $" + withdrawalAmount); return true; } } public static String getAccount(Customer c, Scanner keyboard){ int index = search(c); if (index == -1) { System.out.println("Customer does not exist in the system."); } else { return accountList.get(index).toString(); } return "Please try again"; } public static void printAllAccounts(){ int i; for (i = 0; i < accountList.size(); i++){ System.out.println(accountList.get(i)); } } }
public class Customer { private String name; private String address; private int age; private String telephoneNumber; private int customerNumber; public Customer(){ } public Customer(String n, String a, int y, String t){ name = n; address = a; age = y; telephoneNumber = t; customerNumber = customerNumber + 1; } public String getName(){ return name; } public String getAddress(){ return address; } public int getAge(){ return age; } public String getTelephoneNumber(){ return telephoneNumber; } public int getCustomerNumber(){ return customerNumber; } public void setname(String n){ name = n; } public void setAddress(String a){ address = a; } public void setAge(int y){ age = y; } public void setTelephoneNumber(String t){ telephoneNumber = t; } public String toString(){ return null; } public boolean equals(Customer c){ return name.equals(c.name); } }
public abstract class Account{ Customer customer; private double balance; private int accountNumber; public Account(){ } public Account(Customer c, double b){ customer = c; balance = b; accountNumber = accountNumber + 1; } public double getBalance(){ return balance; } public int getAccountNumber(){ return accountNumber; } public void setBalance(double b){ balance = b; } public String toString(){ return customer + "~" + balance; } }
public class CheckingAccount extends Account { private final static double MONTHLY_FEE = 0.0; public CheckingAccount(Customer c, double b){ super(c, b); } public double getMonthlyFee(){ return MONTHLY_FEE; } public void deductFee(){ } }
public class SavingAccount extends Account { private final static double INTEREST_RATE = 0.0; public SavingAccount(Customer c, double b){ super(c,b); } public double getInterestRate(){ return INTEREST_RATE; } public void depositInterest(){ } }