Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointException in Simple Program, Cannot Find Fix
    primarykey
    data
    text
    <p>This is the exception I am getting:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at BankAccountDemo.DisplayAccountFees(BankAccountDemo.java:91) at BankAccountDemo.main(BankAccountDemo.java:30) </code></pre> <p>I am also getting this exception whenever I try to print or view all the accounts with the fees assessed. </p> <p>Here is my driver program. It has 5 classes, which are also below. One of them is an abstract class with 3 descendants. </p> <p>DRIVER</p> <pre><code>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 &lt; 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 &lt; 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 &lt; 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 &lt; savingsAccounts.size(); n++) { Savings aSavings = savingsAccounts.get(n); aSavings.DisplayData(); } for(int n=0; n &lt; checkAccounts.size(); n++) { Checking aChecking = checkAccounts.get(n); aChecking.DisplayData(); } for(int n=0; n &lt; 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 &lt; loanAccounts.size(); n++) { Loan aLoan = loanAccounts.get(n); aLoan.PrintAccountToFile(printFile); } for (int n=0; n &lt; checkAccounts.size(); n++) { Checking aChecking = checkAccounts.get(n); aChecking.PrintAccountToFile(printFile); } for (int n=0; n &lt; savingsAccounts.size(); n++) { Savings aSavings = savingsAccounts.get(n); aSavings.PrintAccountToFile(printFile); } printFile.close(); fileOutput.close(); } catch(IOException ex) { } } private static ArrayList&lt;Checking&gt; checkAccounts = new ArrayList&lt;Checking&gt;(); private static ArrayList&lt;Savings&gt; savingsAccounts = new ArrayList&lt;Savings&gt;(); private static ArrayList&lt;Loan&gt; loanAccounts = new ArrayList&lt;Loan&gt;(); } </code></pre> <p>ACCOUNT CLASS</p> <pre><code>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()); } } </code></pre> <p>CHECKING CLASS</p> <pre><code>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) { } } } </code></pre> <p>LOAN CLASS</p> <pre><code>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) { } } } </code></pre> <p>SAVINGS CLASS</p> <pre><code>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 &lt; 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) { } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload