I am working on a bank account program for my Java class. I am still very new to this, and this is our first assignment working with arrays. So please excuse any rookie errors lol. The assignment requires an array of 3 bank accounts for now. I need 3 classes; Bank (which contains the bankAcct myAcct[] = new bankAcct), bankUser, and bankAcct(which contains my withdraw, deposit and see balance methods). As of now I am getting a NullPointerException in my proChoice method anytime I try to view balance, deposit, or withdraw.The error is in my User class, I will leave comments in the code to show exactly where. Please, any help would be greatly appreciated. I am reading my textbook but not really finding much that can help my particular issue. Thank you in advance.
bankAcct Class
import java.util.Scanner;
public class bankAcct
{
private double Bal;
private int acctNum;
private String name;
Scanner scannerObject = new Scanner(System.in);
public bankAcct(int pacctNum, double pBal, String pname) {
pBal = Bal;
pacctNum = acctNum;
pname = name;
}
public void makeDeposit()
{
System.out.print( "Hello " + name + ", please, enter amount to deposit $");
double lDep;
lDep = scannerObject.nextDouble();
Bal = Bal + lDep;
System.out.println( " You have deposited $" + lDep);
System.out.println( " Your new balance is $" + Bal);
}
public void makeWithdrawal()
{
System.out.print( "Hello " + name + ", please, enter amount to withdraw $");
double lWDraw;
lWDraw = scannerObject.nextDouble();
if (lWDraw <= Bal){
Bal = Bal - lWDraw;
System.out.println( "You have withdrawn $" + lWDraw);
System.out.println( "Your new balance is $" + Bal);
}else{
System.out.println("Insufficient funds!");
}
}
public void dispBal()
{
System.out.println( "Your current balance is $" + Bal);
}
public void setAcctNum(int pacctNum)
{
pacctNum = acctNum;
}
public int getAcctNum()
{
return acctNum;
}
public void setName(String pname)
{
pname = name;
}
public String getName()
{
return name;
}
}
Bank Class
import java.util.Scanner;
public class Bank
{
int max = 3;
int count;
bankAcct myAcct[] = new bankAcct[max];
bankUser user = new bankUser();
Scanner scannerObject = new Scanner(System.in);
public void openAcct()
{
String lname;
if (count >= max){
System.out.println("Not accepting new customers at this time.");
}else{
System.out.println("Please enter your name: ");
lname = scannerObject.nextLine();
myAcct[count] = new bankAcct(count + 1, 0, lname);
count++;
System.out.println("Thank you " + lname + ", your account number is: " + count);
}
}
public int findAcct()
{
int lnum = -1;
System.out.println("Greetings, please enter your account number: ");
lnum = scannerObject.nextInt();
for(count = 0; count < max; count++){
if (count == lnum)
return count;
}
return lnum;
}
public void seeBal()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].dispBal();
}
}
void Deposit()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].makeDeposit();
}
}
void Withdrawal()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].makeWithdrawal();
}
}
}
User Class
import java.util.Scanner;
public class bankUser
{
public static void main(String[] args)
{
Bank myBank = new Bank();
Scanner scannerObject = new Scanner(System.in);
int Choice;
do
{
dispMenu();
Choice = getChoice(scannerObject);
proChoice(Choice, myBank); ***//Error occurring here***
}
while (Choice !=0);
}
public static void dispMenu()
{
System.out.println( "|==================================|");
System.out.println( "| TONY'S FIRST NATIONAL BANK |");
System.out.println( "|***********Menu Options***********|");
System.out.println( "|__________________________________|");
System.out.println( "| Press 1 To Open New Account |");
System.out.println( "| Press 2 To View Balance |");
System.out.println( "| Press 3 To Make Deposit |");
System.out.println( "| Press 4 To Make Withdrawal |");
System.out.println( "| Press 0 to Exit |");
System.out.println( "|__________________________________|");
System.out.println( "| Please Make Selection Now... |");
System.out.println( "|==================================|");
}
static int getChoice(Scanner scannerObject)
{
int pChoice, Choice;
pChoice = scannerObject.nextInt();
Choice = pChoice;
return Choice;
}
static void proChoice(int Choice, Bank myBank)
{
switch (Choice)
{
case 1: myBank.openAcct();
break;
case 2: myBank.seeBal(); //***Error Here***
break;
case 3: myBank.Deposit(); //***Error Here***
break;
case 4: myBank.Withdrawal(); //***Error Here***
break;
case 0: System.out.println( "Thank you, come again.");
break;
}
}
}