1

I'm having a problem with my program. Currently, I'm making a billing account system. Basically, I've got a great deal of the system up and running. As a feature, rather than having a user remember their position in an array, a user could carry out actions for their account by entering their account number. So, in other words, they would be prompted to enter an account number and any actions are attributed to that account.

Here is the code I have so far:

intEntry = input.nextInt();
for (count = 0; count <= ACCLIMIT; count++)
{
    if (intEntry == NewAccount[count].getAccRefNo() )
    {
        intSelectedEntry = count;
    }//end of if statement
    else
    {
        System.out.println("Invalid ID!");
    }//end of else statement
}//end of loop
System.out.println("*******Please enter the amount you wish to deposit*******") ;
valDeposit = getBalanceValidation();
parDepositAmount = Double.valueOf(valDeposit).doubleValue ();
NewAccount[intSelectedEntry].deposit(parDepositAmount);

When I run it, it crashes once I enter the ID number intEntry. It says the error is in the line of the if statement criteria, if that helps. Please be aware I'm really new to Java, and I'd really appreciate this help explained in a simple way. (Thanks!)

Here is the error message:

Exception in thread "main" java.lang.NullPointerException
at pkgGasAccount.UsingBusinessAccount.main(UsingBusinessAccount.java:106)

Java Result: 1

Line 106 is the first line of the if statement (the criteria)

4

2 回答 2

0

NewAccount[count]null

您应该检查NewAccount[count] != null

if (NewAccount[count]!= null && intEntry == NewAccount[count].getAccRefNo() )

但是,如果您不期望null那里的值,我建议您检查为什么会发生这种情况。

于 2013-04-28T20:55:00.137 回答
0

NullPointerException 被抛出,因此我可以说您的代码正在尝试访问未定义的数组或指向空值(默认)

由于只有一个数组 NewAccount[],因此我会检查相同的声明。

于 2013-04-28T21:00:20.463 回答