Sorting an array

Why won't this work?

Data[] strArray =new Data[5];

java.util.Arrays.sort(strArray);

Exception in thread "main" java.lang.NullPointerException

at java.util.Arrays.mergeSort(Unknown Source)

at java.util.Arrays.sort(Unknown Source)

at Lab7.main(Lab7.java:100)

[334 byte] By [johnp11a] at [2007-10-3 10:15:15]
# 1

Data[] strArray = new Data[5];

java.util.Arrays.sort(strArray);

so, in this case, what does Data[0] equal? Or Data[1]?

I'll give you a hint: null

Now consult your error message: NullPointerException .... hmmmm...

How do you think you would remedy that?

Navy_Codera at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 2

When you create a new array of T type objects (where T is Data in your

example) that array can potentially store n references to a T type. Those

array elements point to nothing yet, i.e. you have to assign a valid T object

reference to every array element. As long as you haven't done so every

element doesn't refer to anything, iow the references equal null.

kind regards,

Jos

JosAHa at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 3

Because the array holds 5 null values.

When you invoke Arrays.sort(...) the compareTo(...) method of all elements in the array will be called. And null.compareTo(...) will throw a NPE.

Message was edited by:

prometheuzz

Jos, I don't want to hear it!

:(

prometheuzza at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 4
@prometheuzz: a young bright man like you being so slow? tsk tsk.kind regards,Jos ;-)
JosAHa at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 5
> @prometheuzz: a young bright man like you being so> slow? tsk tsk.> > kind regards,> > Jos ;-)Yeah yeah, rub it in, you old geezer!; )
prometheuzza at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 6
> @prometheuzz: a young bright man like you being so> slow? tsk tsk.> > kind regards,> > Jos ;-)so what was your excuse ye sage of coding? ;-)
Navy_Codera at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 7
> so what was your excuse ye sage of coding? ;-)Jos calls it a victory when he's not last.This usually happens when I'm around.; )
prometheuzza at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 8
How do i assign a valid Data object reference to the array?
johnp11a at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 9

Assign a value != null to the elements of your array...

int[] data = { 5, 6, 7, 8, 9 };

or

int[] data = new int[5];

for (int i = 0; i < 5; i++) data[i] = i;

Or myriad other ways. Whatever strikes your fancy.

Navy_Codera at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 10

> > @prometheuzz: a young bright man like you being so slow? tsk tsk.

> >

> > kind regards,

> >

> > Jos ;-)

>

> so what was your excuse ye sage of coding? ;-)

I have no excuse Sir because I don't need one; I was still reflecting on

the optimal number of deer present in a venison sausage for which you

Sir, until now have stayed in vain and still haven't supplied a satisfactory

answer.

kindest regards,

Jos ;-)

JosAHa at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 11

> > so what was your excuse ye sage of coding? ;-)

>

> Jos calls it a victory when he's not last.

> This usually happens when I'm around.

> ; )

Are you feebly trying to conquor my well established reputation for being

the slowest old sod here young man? hm?

kind regards,

Jos (< slowest old sod; proven; so there ;-)

JosAHa at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 12

I don't really get this part. I can add data and read data from Data[] strArray = new Data[5];

but it tells me that the data does not exist when i want to sort it?

add data..

strArray[next] = new Data(data);

read data...

for (int i = 0; i < next; i++) {

System.out.println(strArray);

}

johnp11a at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 13

> I don't really get this part. I can add data and

> read data from > Data[] strArray = new Data[5];

> but it tells

> me that the data does not exist when i want to sort it?

If that is so, something else in your code is wrong, i.e. if you get

sensible results printed for every element of your Data array while the

Array.sort() method complains you have to show us some more code.

kind regards,

Jos

JosAHa at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 14
I'm OUTTA here! (Wife and I are off to a movie for the first time in 3 YEARS!!!) We're going to see Borat.You all have a lovely evening/morning/day (whatever the case may be for your respective timezones)!
Navy_Codera at 2007-7-15 5:35:53 > top of Java-index,Java Essentials,Java Programming...
# 15
Enjoy the evening, the movie and all; Borat is fun.kind regards,Jos
JosAHa at 2007-7-21 13:09:12 > top of Java-index,Java Essentials,Java Programming...