Now after editing my program as per suggestion I am getting no display message while running the program. AccountBalance.java is getting compiled and AccountBalance.class file is getting generated but when the program is being runned its displaying nothing only cursor is blinking.
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);
}
}
When I am trying to run the program I am getting the following error:
"Exception in thread "main" java.lang.NullPointerException"
I am trying to write a program which has 2 classes among which 1 is having the main method. Minimum amount to open bank account is Rs. 1000, Opening amount, Bank Account number deposit and withdrawal are inputted by user. Need to display current Account Balance and whether a person to continue the account or not he/she can select an option to close it and the program will terminate. What I am doing wrong.. how to counter it and fix it...?
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);
}
}