将对象数组传递给静态方法时,出现空指针异常

时间:2018-10-21 22:15:11

标签: java arrays

我是编程新手,并且是第一次使用对象数组。

我有一个称为Account的类,它代表一个银行帐户。在我的ATM机类的主要方法中,我创建了一个Account对象数组。我通过静态方法用实例化的对象填充数组。

我有两种其他的静态方法(一种打印出阵列,我将其放入测试中,另一种假设是检查银行帐户ID是否有效)。当将Account对象的数组传递给这些相应的静态方法时,这两个静态方法都将引发Null Pointer Exception。

当我注释掉这两个静态方法时,一切正常。任何帮助,将不胜感激。代码如下:

package banking;

import java.util.Scanner;

public class AtmMachine {
    public static void main(String[] args) {
        //Create account array of size 10
        Account[] bankAccounts = new Account[10];

        //Create 10 accounts
        createAccounts(bankAccounts);

        /*
        //PROBLEM AREA
        //Print Bank Accounts
        printAccounts(bankAccounts);
        */


        //Menu
        menu(bankAccounts);

    }

    public static void createAccounts(Account[] bankAccounts) {
        for (int i = 1; i < bankAccounts.length; i++) {
            bankAccounts[i] = new Account(i, 100);
        }
    }

    public static void menu(Account[] bankAccounts) {
        //Create Scanner object
        Scanner input = new Scanner(System.in);

        while (true) {
            System.out.print("Enter your account number: ");
            int accountNumber = input.nextInt();



            //THIS IS THE PROBLEM
            /*
            //Check to see if a valid account
            if (!isAccount(accountNumber, bankAccounts)) {
                System.out.println("You entered a wrong account number.  Try again.");
                continue;
            }
            */


            //Enter the menu of the account system
            accountMenu(bankAccounts[accountNumber]);


        }
    }

    //THE PROBLEM METHOD
    public static boolean isAccount(int account, Account[] bankAccounts) {
        for (int i = 0; i < bankAccounts.length; i++) {
            if (bankAccounts[i].getId() == account) {
                return true;
            }
        }
        return false;
    }

    public static void accountMenu(Account myAccount) {
        Scanner input = new Scanner(System.in);

        System.out.println("Main Menu: ");
        System.out.println("1. Check Balance");
        System.out.println("2. Withdraw");
        System.out.println("3. Despoit");
        System.out.println("4. Exit");

        int selection = input.nextInt();

        while (selection < 4) {
            if (selection == 1) {
                System.out.printf("The balance is %.2f.\n", myAccount.getBalance());
            } else if (selection == 2) {
                System.out.print("Enter the amount to withdraw: ");
                myAccount.withdraw(input.nextDouble());
                System.out.printf("The new balance is %.2f\n", myAccount.getBalance());
            } else if (selection == 3) {
                System.out.print("Enter the amount to deposit: ");
                myAccount.deposit(input.nextDouble());
            } else {
                System.out.println("You entered a wrong amount.");
                continue;
            }

            System.out.println("Main Menu: ");
            System.out.println("1. Check Balance");
            System.out.println("2. Withdraw");
            System.out.println("3. Despoit");
            System.out.println("4. Exit");

            selection = input.nextInt();


        }

    }

    public static void printAccounts (Account[] bankAccounts){
        for(int i = 0; i < bankAccounts.length; i++){
            System.out.println("Account " + i + " has an id of " + bankAccounts[i].getId() + " and a balance of " + bankAccounts[i].getBalance());
        }
    }

}

1 个答案:

答案 0 :(得分:3)

createBankAccounts()方法中,您将从1开始而不是从0开始for循环。Java数组从0开始索引,这意味着它们从0开始。由于for循环从1开始,因此循环中的第一个元素数组永远不会初始化,导致它抛出NullPointerException

更改此:

public static void createAccounts(Account[] bankAccounts) {
    for (int i = 1; i < bankAccounts.length; i++) {
        bankAccounts[i] = new Account(i, 100);
    }
}

为此:(int i = 1变成int i = 0

public static void createAccounts(Account[] bankAccounts) {
    for (int i = 0; i < bankAccounts.length; i++) {
        bankAccounts[i] = new Account(i, 100);
    }
}