Java again....

Associate
Joined
6 Dec 2007
Posts
2,103
Hey all (again), this time I was trying to write a piece of code to sort some details, and a line in particular keeps throwinb up a NullPointerException, and I can't figure out why. My head hurts.

public void sortAccountDetails()
{
int iAccountNumber;

for (int i3 = 0; i3 < accountDetails.length -1; i3++)
{
int minPos = i3;
for ( int j3 = i3 +1; j3 < accountDetails.length; j3++ )
{
iAccountNumber = Integer.parseInt( accountDetails [ j3 ].getAccountNumber() );


if ( iAccountNumber < Integer.parseInt( accountDetails [ j3 ].getAccountNumber()))
{
minPos = j3;
}//end if
}//end for

The bold bit is giving me jip. It's not the parseInt doing funny things to my array is it? Thanks
 
It would help if you could show us the contents of the accountDetails[] array. For example:

{"Some String", "Another String", 3, 123.00, 'c'}

I suspect it is the for-loop causing one of your statements to access an invalid index in the array, learning how to use the debugger can save an awful lot of time in these situations because you can execute your code step by step and see what the values of the variables are before the exception is generated and therefore identifying the culprit.
 
Ok, sorry, basically the array contains details that are read in from a text file, details are:

12345678
T Jowell
270.50

34781234
T Blair
100.00

11112222
J Bloggs
250.00

44448888
L Hamilton
1200.00

22228888
G Brown
150.00

********

With the asterisks as a terminator.

So, as far as I can see it shouldn't be null, but that's what its giving me...
 
Either the element at j3 is null, or getAccountNumber invoked on the element at j3 is returning null.

If j3 was an out of bound index he would get an index out of bounds exception. He's just not inserted what he thinks he has into the array correctly, or has fluffed up the getAccountNumber method. That's my guess at least. Can you show us the array initialisation code please?
 
Last edited:
Ok, this bit is the bit that reads in the info from the text file into an array:

public void loadAccountDetails( String filename ) throws IOException
{

String anAccountNumber;
String anAccountName;
double aBalance;
String textFilesPath = "H:/";
BufferedReader accountsFileIn = Text.open(textFilesPath + filename);

activeAccounts = 0;
anAccountNumber = Text.readString(accountsFileIn);
while (anAccountNumber.compareTo("********") != 0 && false && activeAccounts < MAX_ACCOUNTS)
{
anAccountName = Text.readLine(accountsFileIn);
aBalance = Text.readDouble(accountsFileIn);

accountDetails [activeAccounts] = new BankAccount(anAccountNumber, anAccountName, aBalance);
activeAccounts++;
anAccountNumber = Text.readLine(accountsFileIn);
}

accountsFileIn.close();

I'm not too sure where I've gone wrong, but its driving me nuts! Java n00b here lol. Thanks for any help you can give me!

Edit: all getAccountNumber does is return accountNumber
 
Last edited:
I've changed the read-in code to the following, but I still get the same exception. I must be doing something wrong! LOL

activeAccounts = 0;
anAccountNumber = Text.readString(accountsFileIn);
while (anAccountNumber.compareTo("********") != 0 && false && activeAccounts < MAX_ACCOUNTS)
{
anAccountName = Text.readLine(accountsFileIn);
aBalance = Text.readDouble(accountsFileIn);

if (accountDetails[activeAccounts] == null)
{
System.out.println("Null entry");
}

else
{
accountDetails [activeAccounts] = new BankAccount(anAccountNumber, anAccountName, aBalance);
activeAccounts++;
anAccountNumber = Text.readLine(accountsFileIn);
}

}

accountsFileIn.close();
sortAccountDetails();
 
Code:
while (anAccountNumber.compareTo("********") != 0 && false && activeAccounts < MAX_ACCOUNTS)

This statement can never evaluate to true so the code in your loop is never executed. Remove the the "&& false"

Code:
              if (accountDetails[activeAccounts] == null)
                {
                    System.out.println("Null entry");
                }
                
            else
                {
                   [color=red]How about also putting a println here so you
                   can see when the code is correctly running.[/color]

                   //populate data structure
                }
 
Also you might always find (accountDetails[activeAccounts] == null) to be true. I can't see how you've initialised the accountDetails array but it looks like the null check is before you have assigned anything to it.

As per this, apart from any syntax errors I may have made :)

Code:
BankAccount[] accountDetails = new BankAccount[10];
accountDetails[0] == null [color=red]true[/color]
accountDetails[1] == null [color=red]true[/color]

BankAccount[] accountDetails = new BankAccount[10];
for (int i=0; i<accountDetails.length(); i++) {
  accountDetails[i] = new BankAccount();
}
accountDetails[0] == null [color=red]false[/color]
accountDetails[1] == null [color=red]false[/color]
 
Unit tests are for testing units of code (methods) not individual conditions within those units. If you narrow a problem down to a single unit via unit testing, you still need to work out exactly where in that unit things are going wrong.

Besides I'm pretty sure unit testing is beyond the OP at the moment :)
 
Last edited:
Back
Top Bottom