gpt4 book ai didi

java - 为什么我的程序没有运行并抛出 "Exception in thread "main"java.lang.NullPointerException"

转载 作者:行者123 更新时间:2023-11-30 06:50:27 26 4
gpt4 key购买 nike

现在,按照建议编辑我的程序后,我在运行程序时没有收到任何显示消息。 AccountBalance.java 正在编译,AccountBalance.class 文件正在生成,但是当程序运行时,它什么也不显示,只有光标在闪烁。

class Balance {
private Scanner bank;
public int userAccount;
public int bankAccountNumber;

void account()
{ bank = new Scanner(System.in);
int openingAmount = bank.nextInt();
System.out.print("Please deposit an amount more than Rs. 1000.00 to open a Bank account: " + openingAmount);

if (openingAmount > 1000) {
System.out.println("Your Bank account has opened successfully");
userAccount = openingAmount;

System.out.println("Enter your Bank account number : ");
bank = new Scanner(System.in);
bankAccountNumber = bank.nextInt();
} else{ System.out.println("Bank account not opened.\nDo you want to open a bank account then..!!");
this.account(); //Ask again for opening an account
}
}
void withdrawal() {
bank = new Scanner(System.in);
int w = bank.nextInt();
int b = userAccount - w;
System.out.println("Withdrawal Amount is : " + w );
if (w < 100)
{
System.out.println("Unable to process your request");
} else {
System.out.println("Your Balance Amount is : " + b );
}
}

void deposit() {
bank = new Scanner(System.in);
int d = bank.nextInt();
System.out.println("Deposited amount is : ");
userAccount += d;
System.out.println("Your Balance Amount is : " + userAccount );
}
}

public class AccountBalance {
public static void main(String[] args) {
Balance s = new Balance();
s.account();
s.withdrawal();
s.deposit();
System.out.println(" Current account balance is : "+s.userAccount);
}
}

当我尝试运行该程序时,出现以下错误:

“线程“main”java.lang.NullPointerException 中出现异常”

我正在尝试编写一个有 2 个类的程序,其中 1 个类具有 main 方法。开设银行账户的最低金额为卢比。 1000, 开户金额、银行帐号、存取款由用户输入。需要显示当前帐户余额以及一个人是否继续该帐户,他/她可以选择一个选项来关闭它,程序将终止。我做错了什么......如何应对并解决它......?

import java.util.Scanner;
class Balance
{
private Scanner bank;
public int userAccount;
public int bankAccountNumber;
public int balance;

public void account()
{
balance = bank.nextInt();
System.out.print("Please deposit an amount of minimum Rs. 1000.00 to open a Bank account : ");

if (balance >= 1000)
{
System.out.println("Account opened successfully");
bankAccountNumber = bank.nextInt();
System.out.println("Enter your 4 digit Bank account number : ");
} else {
System.out.println("Bank account not opened.\nDo you want to open a bank account then..!!");
this.account(); //Ask again for opening an account
}
}
void withdrawal() {
int w = bank.nextInt();
int b = userAccount - w;
System.out.println("Withdrawal Amount is : " + w );
if (w < 100)
{
System.out.println("Unable to process your request");
} else {
System.out.println("Your Balance Amount is : " + b );
}
}

void deposit() {
int d = bank.nextInt();
System.out.println("Deposited amount is : ");
userAccount += d;
System.out.println("Your Balance Amount is : " + userAccount );
}
}

public class AccountBalance {
public static void main(String[] args) {
Balance s = new Balance();
s.account();
s.withdrawal();
s.deposit();
System.out.println("Your current balance is : " + s.userAccount);
}
}

最佳答案

为您的 Balance 类编写一个构造函数并在其中初始化 Scanner。

    private Scanner bank;

扫描仪已定义但未初始化。将此构造函数添加到您的 Balance 类中。

public Balance()
{
bank = new Scanner(System.in);
}

关于java - 为什么我的程序没有运行并抛出 "Exception in thread "main"java.lang.NullPointerException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42911947/

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