开发者

NullPointException in Simple Program, Cannot Find Fix

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开发者_StackOverflow 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)
    {
    }

}


}


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜