ssc85
ssc85
2017-10-08 12:21

新手在线等~Exception in thread ~

40
  • exception
  • 指针与数组
  • java
  • 指针

package banking;

public class Account {
protected double balance;

public Account(double balance) {
    super();
    this.balance = balance;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public boolean deposit(double amt) {
    balance+=amt;
    return true;
}
public boolean withdraw(double amt) {
    if(amt<=balance) {
        balance -= amt;
    }else {
        return false;
    }
    return true;

}

}

package banking;

public class SavingAccount extends Account {
private double interestRate;

public SavingAccount(double balance, double interestRate) {
    super(balance);
    this.interestRate = interestRate;
}

public double getInterestRate() {
    return interestRate;
}

}

package banking;

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

public boolean withdraw(double amt) {
    if(amt<=balance) {
        balance -= amt;
        return true;
    }else {
        if(overdraftProtection>= amt - balance) {
            overdraftProtection -= amt - balance;
            balance = 0;
        }else {
            return false;
        }
    }
    return true;
}

}

package banking;

public class Customer {
private String lastName;
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}

private String firstName;
Account[] accounts;
 int numberOfAccounts;
public Customer(String L,String F) {

    lastName = L;
    firstName =F;
}
public Customer() {

    accounts = new Account[2];
}

public void addAccount(Account account) {

    accounts[numberOfAccounts++]=account;
}
public Account getAccounts(int index) {
    return   accounts [index];
}


public int getNumberOfAccounts() {
    return numberOfAccounts;
}

}

package banking;

public class Bank {
private Customer[] customers;
private int numberOfCustomers;
public Bank() {
customers = new Customer[5];
}
public Customer getCustomers(int index) {
return customers[index];
}
public int getNumberOfCustomers() {
return numberOfCustomers;
}
public void addCustomer(String L,String F) {
Customer customer = new Customer(L,F);
customers[numberOfCustomers]=customer;
numberOfCustomers++;
}

}

package banking;

public class TestBanking {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;

    bank.addCustomer("Jane", "Smith");
    customer = bank.getCustomers(0);
    customer.addAccount(new SavingAccount(500,0.05));
    customer.addAccount(new CheckingAccount(200,400));

    bank.addCustomer("oven", "Bryant");
    customer = bank.getCustomers(1);
    customer.addAccount(new CheckingAccount(200));

    bank.addCustomer("Tim", "Soley");
    customer=bank.getCustomers(2);
    customer.addAccount(new SavingAccount(1500,0.05));
    customer.addAccount(new CheckingAccount(200));

    bank.addCustomer("Maria", "Soley");
    customer = bank.getCustomers(3);
    customer.addAccount(bank.getCustomers(2).getAccounts(1));
    customer.addAccount(new SavingAccount(150,0.05));

    for(int i = 0;i<bank.getNumberOfCustomers();i++) {
        customer = bank.getCustomers(i);
        System.out.println();
        System.out.println("Customer "+customer.getLastName()+customer.getFirstName());

    }








}

}
错误是Exception in thread "main" java.lang.NullPointerException
at banking.Customer.addAccount(Customer.java:28)
at banking.TestBanking.main(TestBanking.java:13)
不明白哪里错了,刚刚自学JAVA的小萌新,希望能够详细解答,并给出修改方案~

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答