【发布时间】:2013-12-07 19:40:15
【问题描述】:
我在项目中有以下课程:
- 帐号
- 客户
- 名称:fName 和 lName(都是字符串字段)
- 日期:年、月、日(都是 int 字段)
- 银行:包含帐户集合
- InputReader:从键盘读取输入
Account 对象需要 Customer 对象和期初余额。 Customer 对象需要一个 Name 对象和一个 Date 对象。 Name 对象需要名字和姓氏的字符串 我需要向用户询问创建 Name 和 Date 对象的详细信息,以及期初余额。
我必须通过从用户那里获取相关信息来创建一个新帐户,即它要求用户输入客户的姓名、出生日期等。它会读取用户的回复,创建帐户并将其添加到银行。
当我运行 public void createNewAccount() 方法时,我不断收到一条错误消息“java.lang.NullPointerException”。将不胜感激任何帮助。提前致谢。
下面是我的 Bank 类的源代码。
import java.util.ArrayList;
public class Bank
{
public static final double INTEREST_RATE = 0.012;//1.2%
// instance variables - replace the example below with your own
private ArrayList<Account> accounts;
private InputReader reader;
private Name fullName;
private Date dateOfBirth;
/**
* Constructor for objects of class Bank
*/
public Bank()
{
// initialise instance variables
}
/*
* Adds an existing Account to the bank
* @param account
*/
public void addAccount(Account account)
{
accounts.add(account);
}
public void createNewAccount() {
System.out.println("Please enter your first name: ");
String firstName = reader.readString();
System.out.println("Hello " + firstName + ". " + "What is your last name?");
String lastName = reader.readString();
System.out.println("Your last name is " + lastName);
System.out.println("Please enter your year of birth: ");
int thisYear = reader.readInt();
System.out.println("Please enter your month of birth: ");
int thisMonth = reader.readInt();
System.out.println("Please enter your date of birth: ");
int thisDay = reader.readInt();
Name theName = new Name(firstName, lastName);
Date theDateOfBirth = new Date(thisYear, thisMonth, thisDay);
}
}
【问题讨论】:
-
reader在初始化之前使用。首先完成代码的初始化部分。
标签: java