gpt4 book ai didi

java - 我的空指针异常

转载 作者:行者123 更新时间:2023-12-01 18:43:44 24 4
gpt4 key购买 nike

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

当我尝试使用循环初始化 Account 数组时,ATM 类的构造函数中遇到空指针异常错误。我是 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
}

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

最佳答案

这里的问题是这一行:

私有(private)帐户[] acct = 新帐户[10];

只初始化了10个Account实例的空间,并没有实际构造10个Account实例并将它们放入数组中。

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

关于java - 我的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797480/

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