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]

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?
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
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!
:(
@prometheuzz: a young bright man like you being so slow? tsk tsk.kind regards,Jos ;-)
> @prometheuzz: a young bright man like you being so> slow? tsk tsk.> > kind regards,> > Jos ;-)Yeah yeah, rub it in, you old geezer!; )
> @prometheuzz: a young bright man like you being so> slow? tsk tsk.> > kind regards,> > Jos ;-)so what was your excuse ye sage of coding? ;-)
> 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.; )
How do i assign a valid Data object reference to the array?
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.
> > @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 ;-)
> > 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 ;-)
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);
}
> 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
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)!
Enjoy the evening, the movie and all; Borat is fun.kind regards,Jos
JosAHa at 2007-7-21 13:09:12 >
