银行账户计划。 NullPointerException错误

时间:2015-03-21 00:36:13

标签: java arrays nullpointerexception

我正在为我的Java类开发银行帐户程序。我对此仍然很陌生,这是我们第一次使用数组。所以请原谅任何菜鸟错误大声笑。该作业现在需要一组3个银行账户。我需要3节课;银行(包含bankAcct myAcct [] = new bankAcct),bankUser和bankAcct(包含我的提款,存款和查看余额方法)。截至目前,我在我的proChoice方法中得到NullPointerException,任何时候我尝试查看余额,存款或撤销。错误在我的用户类中,我将在代码中留下注释以准确显示在哪里。请,任何帮助将不胜感激。我正在阅读我的教科书,但并没有真正发现可以帮助解决我的特定问题。提前谢谢。

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;
        }


 }

银行等级

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();
    }
}   
}   

用户类

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;
    }
}
}

1 个答案:

答案 0 :(得分:1)

你有几个问题。我解决了第一个,但它将涉及第二个和第三个。第一个问题是如何创建帐户。您当前的构造函数是:

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

应该是这样的:

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

此处您的值已反转,这使您无法为自己创建的帐户分配帐号,姓氏和余额。

其次,您的findAcctseeBalance方法应如下所示:

    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();
    }
}

您最大的问题是使用findAcct方法。你从未找到过该帐户。这就是你做的:

    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;
    }

如果您进行了更改,它将通过选项2运行,但是您将需要处理一些错误。如果您从本文中吸取教训,您应该能够解决程序中的其他问题。您应该能够使DepositWithdraw方法看起来像seeBalance方法。

这是我跑的输出

    |==================================|
|    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...   |
|==================================|

我希望这有帮助:)