0

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;
    }
}
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Scruffy Nerfherder
  • 189
  • 1
  • 4
  • 15
  • 1
    Have you read this: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). – mins Mar 21 '15 at 00:45
  • 1
    1) You gave a specific problem description, included source code, and clearly made effort to debug - Good! Thank you! 2) I added the "java" tag to specify your language - that helps get you a better answer, faster. 3) In this case, I'd strongly urge you to use the debugger. I don't know *which* Java compiler/debugger/(IDE?) you're using. But definitely step through the code, find exactly where the NPE occurs - and simply look at which variable is *uninitialized* at that point. Good luck! – FoggyDay Mar 21 '15 at 00:55
  • Thank you! I am using Eclipse. I will go through it with thoroughly. I could be missing something obvious, I've had 5 midterms this week. So, I'm a little burnt out lol. Thanks again! – Scruffy Nerfherder Mar 21 '15 at 01:07

1 Answers1

1

You have a couple problems. I solved the first one, but it will relate to the second and third. The first issue is how you create you accounts. Your current constructor is:

    public bankAcct(int pacctNum, double pBal, String pname) 
{
    pBal = Bal;
    pacctNum = acctNum;
    pname = name;
} 

It should be this:

        public bankAcct(int pacctNum, double pBal, String pname) 
    {
        Bal = pBal;
        acctNum = pacctNum;
        name = pname;
    } 

Here you had the values reversed, which prevented you from assigning an account number, last name, and balance to the account you created.

Secondly, your findAcct and seeBalance methods should look like this:

    public bankAcct findAcct()
{
    bankAcct myBankAcct = null;
    System.out.println("Greetings, please enter your account number: ");
    int acctNum = scannerObject.nextInt();
    //make sure you use myAcct.length to ensure you don't get an "ArrayOutOfBoundsIndex" error.
    for(count = 0; count < myAcct.length; count++){
        myBankAcct = myAcct[count];
        if(myBankAcct.getAcctNum() == acctNum){
            return myBankAcct;
        }
    }
    return myBankAcct;
 }

public void seeBal()
{
    bankAcct lfound = findAcct();
    if (lfound == null)
    {
        System.out.println("Error!");
    }else{
        lfound.dispBal();
    }
}

Your biggest problem was with your findAcct method. You never found the account. This is what you did:

    public int findAcct()
{
    int lnum = -1;
    System.out.println("Greetings, please enter your account number: ");
    //you get the users acct #
    lnum = scannerObject.nextInt();
    for(count = 0; count < max; count++){ //here you iterate, but you never "find" the account
        if (count == lnum)
            return count; //then you return the "count" rather than the account itself.
            }
    return lnum;
    }

If you make the changes I made, it will run through choice 2, but there are going to be some errors that you are going to have to deal with. If you take the lesson from this post, you should be able to address the other issues you have in your program. You should be able to make your Deposit and Withdraw methods look like the seeBalance method.

Here is the output from what I ran

    |==================================|
|    TONY'S FIRST NATIONAL BANK    |
|***********Menu Options***********|
|__________________________________|
|  Press 1 To Open New Account     |
|  Press 2 To View Balance         |
|  Press 3 To Make Deposit         |
|  Press 4 To Make Withdrawal      |
|  Press 0 to Exit                 |
|__________________________________|
|   Please Make Selection Now...   |
|==================================|
1
Please enter your name: 
Blaine
Thank you Blaine, your account number is:       1
|==================================|
|    TONY'S FIRST NATIONAL BANK    |
|***********Menu Options***********|
|__________________________________|
|  Press 1 To Open New Account     |
|  Press 2 To View Balance         |
|  Press 3 To Make Deposit         |
|  Press 4 To Make Withdrawal      |
|  Press 0 to Exit                 |
|__________________________________|
|   Please Make Selection Now...   |
|==================================|
2
Greetings, please enter your account number: 
1
Your current balance is $0.0
|==================================|
|    TONY'S FIRST NATIONAL BANK    |
|***********Menu Options***********|
|__________________________________|
|  Press 1 To Open New Account     |
|  Press 2 To View Balance         |
|  Press 3 To Make Deposit         |
|  Press 4 To Make Withdrawal      |
|  Press 0 to Exit                 |
|__________________________________|
|   Please Make Selection Now...   |
|==================================|

I hope this helps :)

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • Thank you very much sir! I am seeing what I did wrong. I was so unsure of what needed to be returned in my find account and my instructor was being so coy about that one method lol. But just one question for bankAcct myBankAcct = null; could I use bankAcct myAcct, or is there a reason that I need a different bankAcct obj here? I just want to make sure I fully understand... But again, THANK YOU! Saved me hours of going crazy lol – Scruffy Nerfherder Mar 21 '15 at 01:45
  • @TonyVega you can't use `myAcct` because that is already declared as an array of `bankAcct`. By doing `bankAcct myBankAcct = null` you are creating a single reference to an account. By making it null, you can check it other methods to make sure it was assigned a value. Feel free to accept if it solved your issue! – BlackHatSamurai Mar 21 '15 at 01:51
  • 1
    Awesome! I understand. Thanks again for all your help. I am still a Padawan learner but hope to get much better... – Scruffy Nerfherder Mar 21 '15 at 02:02