0

This is the exception I am getting:

Exception in thread "main" java.lang.NullPointerException at BankAccountDemo.DisplayAccountFees(BankAccountDemo.java:91) at BankAccountDemo.main(BankAccountDemo.java:30) 

I am also getting this exception whenever I try to print or view all the accounts with the fees assessed.

Here is my driver program. It has 5 classes, which are also below. One of them is an abstract class with 3 descendants.

DRIVER

import java.io.*; import java.util.Scanner; import java.util.ArrayList; public class BankAccountDemo { public static void main(String[] args) { while(true) { Scanner keyboard = new Scanner(System.in); System.out.println("=========Menu========"); System.out.println("1. Add an account."); System.out.println("2. To print the names of all accounts together with the fees assessed"); System.out.println("3. To print all accounts together with owner information"); System.out.println("4. To exit program"); System.out.println("Your choice: "); int input = keyboard.nextInt(); switch(input) { case 1: InputAccount(); break; case 2: DisplayAccountFees(); break; case 3: ShowAccount(); break; default: PrintToFile(); System.exit(0); } } } private static void InputAccount() { System.out.println("Please enter user data as prompted..."); System.out.println("Please enter account type: Press '1' for Checking, '2' for Savings, '3' for Loan"); System.out.println("Account Type: "); Scanner keyboard = new Scanner(System.in); int type = keyboard.nextInt(); switch(type) { case 1: { Checking aChecking = new Checking(); aChecking.AddAccount(); checkAccounts.add(aChecking); break; } case 2: { Savings aSavings = new Savings(); aSavings.AddAccount(); savingsAccounts.add(aSavings); break; } case 3: { Loan aLoan = new Loan(); aLoan.AddAccount(); loanAccounts.add(aLoan); break; } } } private static void DisplayAccountFees() { for (int n=0; n < savingsAccounts.size(); n++) { Savings aSavings = savingsAccounts.get(n); Person aPerson = aSavings.getAccountHolder(); System.out.println("Total Fees for: " +aPerson.getPersonName()); System.out.println("with the account number: " +aSavings.getAcctNumber()); System.out.println("are: " + aSavings.accountFee()); } for (int n=0; n < checkAccounts.size(); n++) { Checking aChecking = checkAccounts.get(n); Person aPerson = aChecking.getAccountHolder(); System.out.println("Total Fees for: " +aPerson.getPersonName()); System.out.println("with the account number: " +aChecking.getAcctNumber()); System.out.println("are: " + aChecking.accountFee()); } for(int n=0; n < loanAccounts.size(); n++) { Loan aLoan = loanAccounts.get(n); Person aPerson = aLoan.getAccountHolder(); System.out.println("Total Fees for: " +aPerson.getPersonName()); System.out.println("with the account number: " +aLoan.getAcctNumber()); System.out.println("are: " + aLoan.accountFee()); } } private static void ShowAccount() { for(int n=0; n < savingsAccounts.size(); n++) { Savings aSavings = savingsAccounts.get(n); aSavings.DisplayData(); } for(int n=0; n < checkAccounts.size(); n++) { Checking aChecking = checkAccounts.get(n); aChecking.DisplayData(); } for(int n=0; n < loanAccounts.size(); n++) { Loan aLoan = loanAccounts.get(n); aLoan.DisplayData(); } } private static void PrintToFile() { try { FileOutputStream fileOutput = new FileOutputStream("Accounts.dat"); ObjectOutputStream printFile = new ObjectOutputStream(fileOutput); for (int n=0; n < loanAccounts.size(); n++) { Loan aLoan = loanAccounts.get(n); aLoan.PrintAccountToFile(printFile); } for (int n=0; n < checkAccounts.size(); n++) { Checking aChecking = checkAccounts.get(n); aChecking.PrintAccountToFile(printFile); } for (int n=0; n < savingsAccounts.size(); n++) { Savings aSavings = savingsAccounts.get(n); aSavings.PrintAccountToFile(printFile); } printFile.close(); fileOutput.close(); } catch(IOException ex) { } } private static ArrayList<Checking> checkAccounts = new ArrayList<Checking>(); private static ArrayList<Savings> savingsAccounts = new ArrayList<Savings>(); private static ArrayList<Loan> loanAccounts = new ArrayList<Loan>(); } 

ACCOUNT CLASS

import java.io.IOException; import java.io.ObjectOutputStream; import java.util.Scanner; public abstract class Account { private String bankName; private String bankAddress; private String acctNumber; private String acctType; private double balance; private Person accountHolder; public Account() { } public Person setAccountHolder(Person holder) { return holder; } public void setBankName(String newBankName) { bankName = newBankName; } public void setAddress(String newBankAddress) { bankAddress = newBankAddress; } public void setAcctNumber(String newAcctNumber) { acctNumber = newAcctNumber; } public void setBalance(double newBalance) { balance = newBalance; } public void setAcctType(String newAcctType) { acctType = newAcctType; } public Person getAccountHolder() { return accountHolder; } public String getBankName() { return bankName; } public String getAddress() { return bankAddress; } public String getAcctNumber() { return acctNumber; } public String getAcctType() { return acctType; } public double getBalance() { return balance; } public abstract double accountFee(); public abstract void DisplayData(); public abstract void AddAccount(); public abstract void PrintAccountToFile(ObjectOutputStream printFile); public void readInputAccount() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the name of your bank: "); bankName = keyboard.nextLine(); System.out.println("Enter the address of your bank: "); bankAddress = keyboard.nextLine(); System.out.println("Enter your account number: "); acctNumber = keyboard.nextLine(); System.out.println("Enter your current balance: "); balance = keyboard.nextDouble(); } public void PrintBankInfo(ObjectOutputStream printFile) { try { printFile.writeUTF("------------------------------------------"); printFile.writeUTF(" Bank information: "); printFile.writeUTF("------------------------------------------"); printFile.writeUTF("Bank name: " + getBankName()); printFile.writeUTF("Bank Address: " + getAddress()); } catch(IOException ex) { } } public void DisplayBankInfo() { System.out.println("------------------------------------------"); System.out.println(" Bank information: "); System.out.println("------------------------------------------"); System.out.println("Bank name: " + getBankName()); System.out.println("Bank Address: " + getAddress()); } } 

CHECKING CLASS

import java.util.Scanner; import java.io.*; public class Checking extends Account { private double monthFee; private int checksAllowed; private int checksUsed; public Checking() { } public double accountFee() { int tooManyChecks = checksUsed - checksAllowed; double fee = monthFee + (3.00 * tooManyChecks); return fee; } public double getMonthFee() { return monthFee; } public int getChecksAllowed() { return checksAllowed; } public int getChecksUsed() { return checksUsed; } public void setMonthFee(double newMonthFee) { monthFee = newMonthFee; } public void setChecksAllowed(int newChecksAllowed) { checksAllowed = newChecksAllowed; } public void setChecksUsed(int newChecksUsed) { checksUsed = newChecksUsed; } public void AddAccount() { Person aPerson = new Person(); aPerson.personInput(); setAccountHolder(aPerson); readInputAccount(); Scanner keyboard = new Scanner(System.in); System.out.println("Please enter the monthly fee: "); monthFee = keyboard.nextDouble(); System.out.println("Please enter the number of free checks allowed: "); checksAllowed = keyboard.nextInt(); System.out.println("Please enter the number of checks used: "); checksUsed = keyboard.nextInt(); } public void DisplayData() { Person aPerson = getAccountHolder(); aPerson.DisplayPersonInfo(); DisplayBankInfo(); System.out.println("------------------------------------------"); System.out.println(" Account information: "); System.out.println("Account Type : Checking"); System.out.println("Bank account number :" + getAcctNumber()); System.out.println("Account balance :" + getBalance()); System.out.println("Monthly fee :" + getMonthFee()); System.out.println("Number of checks used :" + getChecksUsed()); System.out.println("Number of free checks :" + getChecksAllowed()); } public void PrintAccountToFile(ObjectOutputStream printFile) { try { Person aPerson = getAccountHolder(); aPerson.PrintInfo(printFile); PrintBankInfo(printFile); printFile.writeUTF("------------------------------------------"); printFile.writeUTF(" Account information: "); printFile.writeUTF("Account Type : Checking"); printFile.writeUTF("Bank account number :" + getAcctNumber()); printFile.writeUTF("Account balance :" + getBalance()); printFile.writeUTF("Monthly fee :" + getMonthFee()); printFile.writeUTF("Number of checks used :" + getChecksUsed()); printFile.writeUTF("Number of free checks :" + getChecksAllowed()); printFile.writeUTF("Fees accessed : " + accountFee()); } catch(IOException ex) { } } } 

LOAN CLASS

import java.util.Scanner; import java.io.*; public class Loan extends Account { private double monthPayment; private int daysOverDue; public Loan() { } public void setMonthPayment(double newMonthPayment) { monthPayment = newMonthPayment; } public void setDaysOverDue(int newDaysOverDue) { daysOverDue = newDaysOverDue; } public double getMonthPayment() { return monthPayment; } public int getDaysOverDue() { return daysOverDue; } public double accountFee() { double fee = 0.001 * monthPayment * daysOverDue; return fee; } public void AddAccount(){ Scanner keyboard = new Scanner(System.in); Person aPerson = new Person(); aPerson.personInput(); setAccountHolder(aPerson); readInputAccount(); System.out.println("Please enter the monthly payment amount: "); monthPayment = keyboard.nextDouble(); System.out.println("Please enter the number of days past the grace period: "); daysOverDue = keyboard.nextInt(); } public void DisplayData() { Person aPerson = getAccountHolder(); aPerson.DisplayPersonInfo(); } public void PrintAccountToFile(ObjectOutputStream printFile) { try { Person aPerson = getAccountHolder(); aPerson.PrintInfo(printFile); printFile.writeUTF("------------------------------------------"); printFile.writeUTF(" Account information: "); printFile.writeUTF("Account Type: Loan"); printFile.writeUTF("Bank account number: " + getAcctNumber()); printFile.writeUTF("Account balance: " + getBalance()); printFile.writeUTF("Monthly payment amount: " + getMonthPayment()); printFile.writeUTF("Number of days past the grace period: " + getDaysOverDue()); printFile.writeUTF("Fees assessed: " + accountFee()); } catch(IOException ex) { } } } 

SAVINGS CLASS

import java.io.*; import java.util.Scanner; public class Savings extends Account { private double minBal; private double intRate; private double avgBal; public Savings() { } public double accountFee() { double fee = 0.00; if (avgBal < minBal) { fee = 50.00; } return fee; } public double getMinBal() { return minBal; } public double getIntRate() { return minBal; } public double getAvgBal() { return avgBal; } public void setMinBal(double newMinBal) { minBal = newMinBal; } public void setIntRate(double newIntRate) { minBal = newIntRate; } public double getChecks() { return intRate; } public void setChecks(double newIntRate) { intRate = newIntRate; } public void setAvgBal(double newAvgBal) { avgBal = newAvgBal; } public void AddAccount() { Scanner keyboard = new Scanner(System.in); Person aPerson = new Person(); aPerson.personInput(); setAccountHolder(aPerson); readInputAccount(); System.out.println("Please enter the minimum balance: "); minBal = keyboard.nextDouble(); System.out.println("Please enter the average balance: "); avgBal = keyboard.nextDouble(); System.out.println("Please enter interest rate"); intRate = keyboard.nextDouble(); } public void DisplayData() { Person aPerson = getAccountHolder(); aPerson.DisplayPersonInfo(); DisplayBankInfo(); System.out.println("------------------------------------------"); System.out.println(" Account information: "); System.out.println("Account Type: Savings Account"); System.out.println("Bank account number: " + getAcctNumber()); System.out.println("Account balance: " + getBalance()); System.out.println("Minimum balance: " + getMinBal()); System.out.println("Average balance: " + getAvgBal()); System.out.println("Interest rate: " + getIntRate()); } public void PrintAccountToFile(ObjectOutputStream printFile) { try { Person aPerson = getAccountHolder(); aPerson.PrintInfo(printFile); PrintBankInfo(printFile); printFile.writeUTF("------------------------------------------"); printFile.writeUTF(" Account information: "); printFile.writeUTF("Account Type: Savings Account"); printFile.writeUTF("Bank account number: " + getAcctNumber()); printFile.writeUTF("Account balance: " + getBalance()); printFile.writeUTF("Minimum balance: " + getMinBal()); printFile.writeUTF("Average balance: " + getAvgBal()); printFile.writeUTF("Interest rate: " + getIntRate()); printFile.writeUTF("Fees assessed: " + accountFee()); } catch(IOException ex) { } } } 
2
  • Can you tell us what lines lines 30 and 91 are? Commented Apr 19, 2011 at 4:14
  • line 30 is displayaccountfees() Commented Apr 19, 2011 at 4:20

2 Answers 2

6

The problem is that your setAccountHolder method doesn't actually set anything:

Instead of:

public Person setAccountHolder(Person holder) { return holder; } 

it should be:

public void setAccountHolder(Person holder) { this.accountHolder = holder; } 
Sign up to request clarification or add additional context in comments.

1 Comment

For what it's worth, people will be more likely to help you going forward if you accept the answers that actually address your questions.
5

1.) Where do you initialize savingsAccounts attribute?

2.) When you do

DisplayAccountFees() 

you start by doing

Savings aSavings = savingsAccounts.get(n); 

are you sure that get(n) always returns data?

3 Comments

I'm not sure if I understand. Any savings data that's inputted should be stored in the array. .get(n) would print whatever is stored. If no savings data is inputted, would this then give me the error? How do I avoid this? I initialize savingsAccounts as an ArrayList at the bottom.
line 30 is displayaccountfees()
Question 1 is not answered yet. Could it be possible that you are using the variable savingsAccount even if it has no data? About Question 2, I understand the objective of savingsAccounts.get(n), but you are not being "defensive" in your code. You leave it to the user to choose which option should one run, but one could not choose option 1 at first but the 2 or 3 instead... if there are no accounts registered, booom! The variables contain NULL. Question 1 points to the fact I don't see where is savingsAccounts initialized and it could be a permanent NULL. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.