我的java银行程序正在抛出一个坚果点异常,我无法弄清楚原因

时间:2016-06-10 05:19:14

标签: java

例外     线程“main”java.lang.NullPointerException中的异常         在HW3.Bank.createNewCustomer(Bank.java:28)         在HW3.Bank.main(Bank.java:56)“ 就行了     MyCustomers [NumofCustomers] .openAccount(MyFirstName,MyLastName,openingBalence);“

和线     “MyBank.createNewCustomer();”

package HW3;
public class Customer {
private String FirstName;
private String LastName;
private Account MyAccount;

public void openAccount(String MyFirstname, String MyLastname, float OpeningBalence)

{
    MyAccount = new Account();
    if(MyAccount == null);
    {
        FirstName = MyFirstname;
        LastName= MyLastname;
        MyAccount.setMinimumBalence(100);
        MyAccount.setOverdraftFee(45);
        MyAccount.depositFunds(OpeningBalence);
    }
}

public void closeAccount()
{
    MyAccount = null;

}
public void depositFunds(float Funds)
{
    MyAccount.depositFunds(Funds);
}

public void withdrawFunds(float Funds)
{
    MyAccount.withdrawFunds(Funds);
}
public float getBalence()
{
    return MyAccount.getBalence();

}
public void printAccountInfo()
{
    MyAccount.printAccountInfo();
}
public String getFirstName()
{
    return FirstName;
}
public String getLastName()
{
    return LastName;
}
public String getAccountNumber()
{
    return MyAccount.getAccountNumber();
}
}package HW3;

public class Account {

private String AccountNumber;
private float Balence;
private float minimumBalence;
private float overdraftFee;

public float getBalence()
{
    return Balence;
}

public void depositFunds(float Funds)
{
    Balence += Funds;
}
public void withdrawFunds(float Funds)
{
    Balence -= Funds;

    if (Balence<minimumBalence)
    {
            Balence -= overdraftFee;
    }
}   
public void printAccountInfo()
{
    System.out.println("Account number: " + AccountNumber);
    System.out.println("Account Balence: " + Balence);
    System.out.println("Minnimum ballence: " + minimumBalence);
    System.out.println("Account Over draft fee: " + overdraftFee);
}
public String getAccountNumber()
{
    return AccountNumber;
}
public void setMinimumBalence(float MinBalence)
{
    minimumBalence = MinBalence;
}
public void setOverdraftFee(float OverDraft)
{
    overdraftFee = OverDraft;
}

}


package HW3;
import java.util.Scanner;



public class Bank {
    private String RoutingNumber;
    private int NumofCustomers;
    private Customer[] MyCustomers;

public Bank()
{
    this.MyCustomers = new Customer[100];
    NumofCustomers = 0;
}
public void createNewCustomer()
{
    String MyFirstName;
    String MyLastName;
    float openingBalence;
    Scanner userInputScanner = new Scanner(System.in);
    System.out.println("What is your first name? ");
    MyFirstName = userInputScanner.nextLine();
    System.out.println("What is your last name? ");
    MyLastName = userInputScanner.nextLine();
    System.out.println("What is your opening balence? ");
    openingBalence = userInputScanner.nextFloat();
    MyCustomers[NumofCustomers].openAccount(MyFirstName, MyLastName, openingBalence);
}
public int FindCustomer(String MyFirstName, String MyLastName)
{
    for(int i = 0; i< NumofCustomers; i++)
    {
        if((MyCustomers[i].getFirstName() == MyFirstName) && (MyCustomers[i].getLastName()== MyLastName))
        {
            return i;
        }
    }
    return -1;
}
public int FindCustomer(String AccountNum)
{
    for(int i = 0; i< NumofCustomers; i++)
    {
        if(MyCustomers[i].getAccountNumber() == AccountNum)
        {
            return i;
        }
    }
    return -1;
}

public static void main(String args[]) // main function that initiates the helpers
{
    Bank MyBank = new Bank();
    MyBank.createNewCustomer();
    int index = MyBank.FindCustomer("Bijan", "Azodi" );
    if(index >= 0)
    {
        MyBank.MyCustomers[index].depositFunds(1000);
        MyBank.MyCustomers[index].printAccountInfo();
        MyBank.MyCustomers[index].withdrawFunds(500);
        MyBank.MyCustomers[index].withdrawFunds(601);
    }
    else 
    {
        System.out.print("The customer was not found in this bank");
    }
}
}

1 个答案:

答案 0 :(得分:1)

this.MyCustomers = new Customer[100];

不创建一个充满100个客户的数组,它会创建一个可以容纳 100个客户对象的数组。它仍然必须填充Customer对象。