简单程序中的NullPointErexception,无法找到修复程序

时间:2011-04-19 03:59:45

标签: java nullpointerexception

这是我得到的例外:

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

每当我尝试打印或查看评估费用的所有帐户时,我也会收到此异常。

这是我的驱动程序。它有5个类,也在下面。其中一个是带有3个后代的抽象类。

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

}

帐户类

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

}

}

检查课程

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)
    {
    }
    }

}

贷款类

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 个答案:

答案 0 :(得分:6)

问题是您的setAccountHolder方法实际上没有设置任何内容:

而不是:

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

它应该是:

public void setAccountHolder(Person holder) {
    this.accountHolder = holder;
}

答案 1 :(得分:5)

1。)您在哪里初始化 savingsAccounts 属性?

2。)当你这样做时

DisplayAccountFees()

你开始做

Savings aSavings = savingsAccounts.get(n);

你确定get(n)总是返回数据吗?

相关问题