现在根据建议编辑我的程序后,我在运行程序时没有显示消息。 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);
}
}
当我尝试运行程序时,我收到以下错误:
“线程中的异常”主“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);
}
}
答案 0 :(得分:1)
为您的类Balance写一个构造函数,并在其中初始化Scanner。
private Scanner bank;
扫描仪已定义但未初始化。 将此构造函数添加到Balance类。
public Balance()
{
bank = new Scanner(System.in);
}
答案 1 :(得分:0)
在bank = new Scanner(System.in);
方法的开头调用balance = bank.nextInt();
之前尝试添加account
,因为它未初始化,因此它正在创建NullPointerException
。