求助我写了下面的程序但有错:请帮我看一下package exercise2;public class Account { protected double balance; public Account(double init_balance) {
this.balance = init_balance;
} protected double getBalance() {
return balance;
} public boolean deposit(double amt) {
balance += amt;
return true; } public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
return false;
}
}
}
package exercise2;public class SavingsAccount extends Account { private double interestRate; public SavingsAccount(double balance, double interestRate) {
super(balance);
}
}
package exercise2;public class CheckingAccount extends Account { private double overdraftProtection; public CheckingAccount(double balance) {
super(balance); } public CheckingAccount(double balance, double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
} @Override
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
if (amt - balance <= overdraftProtection) {
balance -= amt; return true;
} else {
return false;
} }
}
}
package exercise2;public class Customer { private Account[] accounts;
private String firstname, Lastname;
private static int numberOfAccounts; public Customer(String firstname, String Lastname) {
this.firstname = firstname;
this.Lastname = Lastname;
} public Customer() {
accounts = new Account[3];
} public void addAccount(Account account) {
accounts[numberOfAccounts] = account;
numberOfAccounts++;
} public Account getAccounts(int in_index) {
return accounts[in_index];
} public static int getNumberOfAccounts() {
return numberOfAccounts;
} public String getLastname() {
return Lastname;
} public String getFirstname() {
return firstname;
}
}package exercise2;
public class Bank{
private Customer[] customers;
private static int numberOfCustomers; public Bank() {
customers = new Customer[5];
} public void addCustomer(String firstname,String Lastname)
{
Customer customer=new Customer(firstname, Lastname);
customers[numberOfCustomers]=customer;
numberOfCustomers++; } public static int getNumberOfCustomers() {
return numberOfCustomers;
} public Customer getCustomers(int int_index) {
return customers[int_index];
}}
package exercise2;import java.text.NumberFormat;public class TextBanking { public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer; // Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomers(0); //该客户两个卡
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00)); bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomers(1); //该客户一个卡
customer.addAccount(new CheckingAccount(200.00)); bank.addCustomer("Tim", "Soley");
customer = bank.getCustomers(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00)); //该客户两个卡 bank.addCustomer("Maria", "Soley");
customer = bank.getCustomers(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomers(2).getAccounts(1));//该客户分享上一客户的
//第二个卡
customer.addAccount(new SavingsAccount(150.00, 0.05));//该客户自己又单独有一个
//一共两个 // Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================"); for (int cust_idx = 0; cust_idx < bank.getNumberOfCustomers(); cust_idx++) {
{
customer = bank.getCustomers(cust_idx); System.out.println();
System.out.println("Customer: "
+ customer.getLastname() + ", "
+ customer.getFirstname()); for (int acct_idx = 0; acct_idx < customer.getNumberOfAccounts(); acct_idx++) {
{
Account account = customer.getAccounts(acct_idx);
String account_type = "Savings Account";
if (account instanceof SavingsAccount) {
System.out.println(account_type + ":current balance is" + currency_format.format(account.getBalance())); } else {
System.out.println("Checking Account" + ":current balance is" + currency_format.format(account.getBalance())); } System.out.println();
}
}
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
}
}
}
}错误是这样的
Exception in thread "main" java.lang.NullPointerException
at exercise2.Customer.addAccount(Customer.java:19)
at exercise2.TextBanking.main(TextBanking.java:15)
Java Result: 1但我不知道是怎么错啦该怎么改请帮我看一下谢谢
this.balance = init_balance;
} protected double getBalance() {
return balance;
} public boolean deposit(double amt) {
balance += amt;
return true; } public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
return false;
}
}
}
package exercise2;public class SavingsAccount extends Account { private double interestRate; public SavingsAccount(double balance, double interestRate) {
super(balance);
}
}
package exercise2;public class CheckingAccount extends Account { private double overdraftProtection; public CheckingAccount(double balance) {
super(balance); } public CheckingAccount(double balance, double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
} @Override
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
if (amt - balance <= overdraftProtection) {
balance -= amt; return true;
} else {
return false;
} }
}
}
package exercise2;public class Customer { private Account[] accounts;
private String firstname, Lastname;
private static int numberOfAccounts; public Customer(String firstname, String Lastname) {
this.firstname = firstname;
this.Lastname = Lastname;
} public Customer() {
accounts = new Account[3];
} public void addAccount(Account account) {
accounts[numberOfAccounts] = account;
numberOfAccounts++;
} public Account getAccounts(int in_index) {
return accounts[in_index];
} public static int getNumberOfAccounts() {
return numberOfAccounts;
} public String getLastname() {
return Lastname;
} public String getFirstname() {
return firstname;
}
}package exercise2;
public class Bank{
private Customer[] customers;
private static int numberOfCustomers; public Bank() {
customers = new Customer[5];
} public void addCustomer(String firstname,String Lastname)
{
Customer customer=new Customer(firstname, Lastname);
customers[numberOfCustomers]=customer;
numberOfCustomers++; } public static int getNumberOfCustomers() {
return numberOfCustomers;
} public Customer getCustomers(int int_index) {
return customers[int_index];
}}
package exercise2;import java.text.NumberFormat;public class TextBanking { public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer; // Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomers(0); //该客户两个卡
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00)); bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomers(1); //该客户一个卡
customer.addAccount(new CheckingAccount(200.00)); bank.addCustomer("Tim", "Soley");
customer = bank.getCustomers(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00)); //该客户两个卡 bank.addCustomer("Maria", "Soley");
customer = bank.getCustomers(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomers(2).getAccounts(1));//该客户分享上一客户的
//第二个卡
customer.addAccount(new SavingsAccount(150.00, 0.05));//该客户自己又单独有一个
//一共两个 // Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================"); for (int cust_idx = 0; cust_idx < bank.getNumberOfCustomers(); cust_idx++) {
{
customer = bank.getCustomers(cust_idx); System.out.println();
System.out.println("Customer: "
+ customer.getLastname() + ", "
+ customer.getFirstname()); for (int acct_idx = 0; acct_idx < customer.getNumberOfAccounts(); acct_idx++) {
{
Account account = customer.getAccounts(acct_idx);
String account_type = "Savings Account";
if (account instanceof SavingsAccount) {
System.out.println(account_type + ":current balance is" + currency_format.format(account.getBalance())); } else {
System.out.println("Checking Account" + ":current balance is" + currency_format.format(account.getBalance())); } System.out.println();
}
}
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
}
}
}
}错误是这样的
Exception in thread "main" java.lang.NullPointerException
at exercise2.Customer.addAccount(Customer.java:19)
at exercise2.TextBanking.main(TextBanking.java:15)
Java Result: 1但我不知道是怎么错啦该怎么改请帮我看一下谢谢
//Customer 类下accounts没有实例化
private Account[] accounts;
public void addCustomer(String firstname,String Lastname)
{
Customer customer=new Customer(firstname, Lastname); customers[numberOfCustomers]=customer;
numberOfCustomers++; }这个里面红色部分调用的是
public Customer(String firstname, String Lastname) {
this.firstname = firstname;
this.Lastname = Lastname;
}这个构造函数,里面并没有创建Account 数组,即所创建的Customer并没有将Account 数组初始化即是null,那么对该Customer对象调用addAccount方法就会抛空指针异常了,因为方法里面引用的Account 数组是个空数组
accounts = new Account[3];
}对其进行初始化了吗我还是不理解啊