现在,按照建议编辑我的程序后,我在运行程序时没有收到显示消息。帐户余额.java正在编译,帐户余额.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);
}
}
当我尝试运行该程序时,出现以下错误:
"Exception in thread '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 编写一个构造函数并初始化其中的扫描仪。
private Scanner bank;
扫描程序已定义,但未初始化。将此构造函数添加到 Balance 类中。
public Balance()
{
bank = new Scanner(System.in);
}
尝试在account
方法开头调用balance = bank.nextInt();
之前添加bank = new Scanner(System.in);
,因为它没有初始化,所以它正在创建NullPointerException
。