gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:46:49 24 4
gpt4 key购买 nike

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

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

我有两个额外的静态方法(一个打印出数组,我将其放入测试中,另一个用于检查银行帐户 ID 是否有效)。当 Account 对象数组传递给这些各自的静态方法时,这两个静态方法都会引发空指针异常。

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

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

}

最佳答案

在您的 createBankAccounts() 方法中,您的 for 循环从 1 开始,而不是 0。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);
}
}

关于java - 将对象数组传递给静态方法时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52920387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com