Java 简单程序中的NullPointException,找不到修复程序
这是我得到的一个例外:Java 简单程序中的NullPointException,找不到修复程序,java,nullpointerexception,Java,Nullpointerexception,这是我得到的一个例外: Exception in thread "main" java.lang.NullPointerException at BankAccountDemo.DisplayAccountFees(BankAccountDemo.java:91) at BankAccountDemo.main(BankAccountDemo.java:30) 每当我试图打印或查看所有已评估费用的账户时,我也会遇到这种例外情况 这是我的驱动程序。它有5个类,也在下面。其中一个是
Exception in thread "main" java.lang.NullPointerException
at BankAccountDemo.DisplayAccountFees(BankAccountDemo.java:91)
at BankAccountDemo.main(BankAccountDemo.java:30)
每当我试图打印或查看所有已评估费用的账户时,我也会遇到这种例外情况
这是我的驱动程序。它有5个类,也在下面。其中一个是具有3个子代的抽象类
司机
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.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)
{
}
}
}
储蓄班
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)
{
}
}
}
import java.io.*;
导入java.util.Scanner;
公营储蓄帐户{
私人双明巴尔;
私人双内;
私人双avgBal;
公共储蓄()
{
}
公共双重账户费用()
{
双倍费用=0.00;
如果(avgBal
1.)在哪里初始化savingsAccounts属性
2)当你这样做的时候
DisplayAccountFees()
你从做开始
Savings aSavings = savingsAccounts.get(n);
您确定get(n)总是返回数据吗?问题是您的
setAccountHolder
方法实际上没有设置任何内容:
而不是:
public Person setAccountHolder(Person holder)
{
return holder;
}
应该是:
public void setAccountHolder(Person holder) {
this.accountHolder = holder;
}
我不确定我是否理解。输入的任何储蓄数据都应存储在数组中。get(n)将打印存储的任何内容。如果没有输入储蓄数据,这会给我错误吗?我如何避免这种情况?我将savingsAccounts初始化为底部的ArrayList。第30行是displayaccountfees()问题1尚未回答。即使变量savingsAccount没有数据,您是否可能正在使用它?关于问题2,我理解savingsAccounts.get(n)的目标,但您的代码中没有“防御”。您可以让用户选择应该运行哪个选项,但一开始不能选择选项1,而是选择2或3。。。如果没有注册帐户,booom!变量包含NULL。问题1指出了一个事实,我看不出savingsAccounts在哪里初始化,它可能是一个永久空值。:)你能告诉我们第30行和第91行是什么吗?第30行是displayaccountfees()。就其价值而言,如果你接受的答案能够真正解决你的问题,人们将更有可能帮助你前进。
public void setAccountHolder(Person holder) {
this.accountHolder = holder;
}