null pointer in a method

Hi

i have a switchcase statement in my code that, when selected, this method is called. this method (shown below) finds a person in an array of clients (based on the name the user gives) then displays their info. the problem is that it comes up with a null pointer exception if the array is empty and i thought i had tackled this problem by the very first if statement, buyt this statement isnt noticed by the program? any help as to y this is so would be appreciated. PS: i have commented the chunk out as i have not yet written the code for the accounts bit of the task

publicstaticvoid displayClient (Client[]c)

{

String name;

int total=0, poss=0, choice;

if (c.length == 0)

{

System.out.println("no clients exist");

}

else

{

System.out.print("Please enter the name of the client you wish to show: ");

name = console.next();

for (int i=0;i<total;i++)

{

if ((c[i].getName()) == name)

{

poss = i;

}

}

}

System.out.println("name: "+(c[poss].getName()));

System.out.println("Resident: "+(c[poss].getResident()));

System.out.println("Gross Salary (per week): "+(c[poss].getWeeklyGrossSalary()));

System.out.println("Net Salary (per week): "+(c[poss].getWeeklyNetSalary()));

System.out.println("tax (per week): "+(c[poss].getWeeklyTax()));

System.out.println("medicare (per week): "+(c[poss].getWeeklyMedicare()));

System.out.println("Expenses: "+(c[poss].getWeeklyExpenses()));

//if (a.length=0)

//{

//System.out.println("No Accounts");

//}

// else

// {

// showAccountSelection();

// choice = console.nextInt();

//while(choice != 9)

//{

// switch(choice)

// {

//case 1:

// c.addAccount();

// break;

//case 2:

//c.deleteAccount();

//break;

//case 3:

// c.displayAccount();

// break;

//case 9:

//

// break;

// default: System.out.println("Invalid Selection");

//}//end switch

//showAccountSelection();

// choice = console.nextInt();

//

// }

//}

}

>

[3977 byte] By [Aussie_Nerda] at [2007-11-27 5:20:06]
# 1
Where is the error occurring? If c itself is null, you can't dereference it to get an array's length. Null is not the same as length==0.
jverda at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 2
if ((c[i].getName()) == name)Assuming that name is a String, this is an improper way to test them for equality. You should always use the .equals() method for objects.And yes, also what Jverd said.
JJCoolBa at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 3

c.length == 0

I'd rethink your code here. The only way you can have an array with a length of zero is as below.

Object[] arr = new Object[0];

Which is entirely pointless IMHO.

Object[] arr;

This doesn't create an array at all and thus arr will be null. Could this be the source of your problem?

floundera at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 4

What i thought i was doing with the c.length == o statement was checking to see if the array elements were all empty and if there was no filled element in the array, the user wouldnt be able to find a client if they arent there so therefore the no clients message appears... i may be mistaken with using this code to achieve this goal?

Aussie_Nerda at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 5

> What i thought i was doing with the c.length == o statement was checking to

> see if the array elements were all empty

With c==0 you check the length of the array (whether the array contains valid values is not checked however). What you however fail to check is whether c contains an array at all. As c == null the parameter .length is not even available (as null is not an array).

Just add a check at the beginning of your method to check whether c is null. If it isn't, c is an array and you can run the remainder of your code.

Message was edited by:

EvilBro

EvilBroa at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 6

ok i tried to use

if (c == null)

and it still progresses through the method as before, am i checking if c is null incorrectly? forgive me lol i am only just learning arrays and how to check their elements etc and all other things associated with arrays

Aussie_Nerda at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 7

> ok i tried to use

> if (c == null)

> and it still progresses through the method as before, am i checking if c is null

> incorrectly?

Yes that checks whether c is null.

The NullPointerException can occur for all sorts of reasons - for instance c[poss] could be null after the for loop. Perhaps you should post the code (if it has changed substantially) and the actual message from the compiler.

(Do you realise that your for loop is doing nothing? total is zero at the start of the method and is never changed.)

You might want to rethink the if statement that beginsif ((c[i].getName()) == name)

Use equals() to compare Strings:if (c[i].getName().equals(name))

pbrockway2a at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 8
read carefully... :)> If it ISN'T, c is an array and you can run the remainder of your code.
EvilBroa at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 9
I think Regina knows the solution !!!!!
AsYourMindFliesa at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 10
try this...if(c == null && c <=0)
S_Singha at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 11
> try this...> > if(c == null && c <=0)That can never compile, and it makes no sense.
jverda at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...
# 12
Hi All, sorry i didnt respond but i thought id let yall kno i solved it, i hadnt used a for loop to set each element of the array as a client so there were null elements so it now works :)
Aussie_Nerda at 2007-7-12 11:44:01 > top of Java-index,Java Essentials,New To Java...