我的Null指针异常

时间:2013-09-14 01:43:48

标签: java arrays object

我正在为一个面向对象的编程类介绍一个简单的程序,我有3个类:AccountATM和main / Runner类。

当我尝试使用循环初始化ATM数组时,我在Account类的构造函数中遇到空指针异常错误。我是Java的新手,所以我真的不知道会发生什么。

以下是代码:

import java.util.Date;
public class Account {
    private int account_id;
    private double account_balance;
    private static double annual_interest_rate;
    private Date date_Created;

    Account(){
        account_id = 0;
        account_balance=0;             
        annual_interest_rate = 0;   
        date_Created = new Date();
    }

    Account(int account_id_in, 
        double account_balance_in, double annual_interest_rate_in) {
        account_id           =  account_id_in;
        account_balance      =  account_balance_in;
        annual_interest_rate = annual_interest_rate_in;
        date_Created = new Date();
    }
    int get_account_id(){       
        return account_id;
    }
    double get_account_balance(){
        return account_balance;
    }
    double get_annual_interest_rate(){
        return annual_interest_rate;
    }
    Date get_date_created(){
        return date_Created;        
    }
    void set_account_id(int account_id_in){
        account_id = account_id_in;
    }
    void set_account_balance(double account_balance_in){
        account_balance = account_balance_in;
    }
    void set_annual_interest_rate(double annual_interest_rate_in){
        annual_interest_rate = annual_interest_rate_in;
    }
    double get_monthly_interest_rate(){     
        return annual_interest_rate/12;
    }
    double get_monthly_interest(){
        return (account_balance * get_monthly_interest_rate())/100;
    }
    void perform_deposit(double deposit_in){
        account_balance += deposit_in;
    }
    void perform_withdraw(double withdraw_amount){
        account_balance -= withdraw_amount;
    }
}



public class ATM {
    private Account[] acct = new Account [10];

    public ATM(){
        //acct = new Account[10];
        for(int i = 0; i<10 ; i++){
            acct[i].set_account_id(i+1);
            acct[i].set_account_balance(100); // here iam getting an error.
        }

        //you must set their ids to values as specified in the assignment
    }         //these are the teacher's comments and instructions.

    public void displayMenu(){
        System.out.println("ATM Menu:");
        System.out.println("\tenter 1 for viewing the current balance");
        System.out.println("\tenter 2 for withdrawing money");
        System.out.println("\tenter 3 for for depositing money");
        System.out.println("\tenter 4 for exiting the main menu");
    }

    public boolean checkID(int id){
        for(int i = 0; i<10 ; i++){
            if(acct[i].get_account_id() == id){
                return true;
            }   
        }
        return false;

    }

    public double checkBalance(int idToSearch){
        int indexOfAccountToReturn = 0;
        for(int i = 0; i<10; i++){
            if(acct[i].get_account_id() == idToSearch){
                indexOfAccountToReturn = i;
                break;
            }
        }
        return acct[indexOfAccountToReturn].get_account_balance();
    }

    public void withdrawFunds(int id, double amount){
        for(int i=0; i<10; i++){
            if(acct[i].get_account_id() == id){
                acct[i].perform_withdraw(amount);
                break;
            }
        }
    }

    public void depositFunds(int id, double amount){
        for(int i=0; i<10; i++){
            if(acct[i].get_account_id() == id){
            acct[i].perform_deposit(amount);
            break;
            }
        }   
    }
}

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Banking_Finance_Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ATM atm = new ATM();
        Scanner input = new Scanner(System.in);

        do{
            System.out.println("Account ID?");
            int id = input.nextInt();

            while(!(atm.checkID(id))){
                String entry = 
                                JOptionPane.showInputDialog("Incorrect Input");
                id = Integer.parseInt(entry);
            }
            //prevent user to proceed without the correct id; use checkID method and store appropriately
do{
    atm.displayMenu();
    System.out.println("Your choice?");
    int choice = input.nextInt();

    while((choice >4) || (choice <1)){
    String entry = JOptionPane.showInputDialog("Incorrect Imput");
    choice = Integer.parseInt(entry);
                    }
                //prevent user to proceed without the correct choice 1-4;

    if(choice==1){
    System.out.println("Enter the Account id to check the balance: ");
    int idToSearch = input.nextInt();
    System.out.println("The balance in this ACccount is: $" + atm.checkBalance(idToSearch));

    }else if(choice==2){
    System.out.println("Enter the Account id to perform withdrawal: ");
    int idToSearch = input.nextInt();
    System.out.println("Enter the amount to be withdrawn: ");
    double amountToWithdraw = input.nextDouble();
    atm.withdrawFunds(idToSearch, amountToWithdraw);


    }else if(choice==3){
    System.out.println("Enter the Account id to perform deposit: ");
    int idToSearch = input.nextInt();
    System.out.println("Enter the amount to be deposited: ");
    double amountToDeposit = input.nextDouble();
    atm.depositFunds(idToSearch, amountToDeposit);

    }else{
         break;
    }

    }while(true);
        }while(true);
    }

}

如果我不恰当地使用本网站,我很抱歉,这是我的第一个问题,请耐心等待。 我设法通过以下修复来传递错误:

public ATM(){
    for(int i = 0; i<10 ; i++){
        acct[i] = new Account();
        acct[i].set_account_id(i+1);
        acct[i].set_account_balance(100);
    }

    //you must set their id's to values as specified in the assignment
}

但是我不确定这是否正确。是吗?

2 个答案:

答案 0 :(得分:4)

这里的问题是这一行:

private Account[] acct = new Account [10];

仅初始化10个帐户实例的空间,它实际上并不构建10个帐户实例并将它们放入数组中。

在ATM构造函数的for循环中,您需要先调用acct [i] = new Account();

答案 1 :(得分:0)

除了创建数组之外,还需要用对象填充它。在ATM构造函数中更新代码:

for (int i = 0; i < 10; i++) {
    acct[i] = new Account(); // add this line to crate account
                             // and place it in array
    acct[i].set_account_id(i + 1); // also you are getting error here
    acct[i].set_account_balance(100); // not here because you ware trying to 
                                      // invoke method on `null`
}