array.sort error: java.lang.nullPointerException.

Hi all,

i'm trying to sort an object. My code goes as follows:

public static void sortElemByFitness(){

prepGAGrammar[] gram= new prepGAGrammar[pGA.compoundElem.size()];

try {

BufferedReader in= new BufferedReader(new FileReader(expGAMain.scorefile));

String line;

int temp=0;

while ((line =in.readLine())!= null) {

int ind=line.indexOf(",");

elemNameFromFile.add(line.substring(0, ind));

//System.out.println("temp is:"+temp);

gram[temp]=new prepGAGrammar();

gram[temp].setCompoundElementName(elemNameFromFile.get(temp));

elemFitness.add(Double.parseDouble(line.substring(ind+1)));

gram[temp].setElemFitness(elemFitness.get(temp));

temp++;

}

in.close();

System.out.println("Natural Order");

System.out.println("Index\tElement name\tcomponents\tfitness");

for (int m=0; m<elemNameFromFile.size();m++){

prepGAGrammar g=gram[m];

System.out.print(m+"\t"+g.getCompoundElementName()+ "\t");

for(int n=0; n><pGA.compoundComp.length;n++){

if(pGA.compoundComp[m][n]!=null){

g.setCompoundComponentName(pGA.compoundComp[m][n]);

System.out.print(g.getCompoundComponentName()+" ");

}

}

System.out.print(g.getElemFitness());

System.out.println();

}

Arrays.sort(gram,desc);

System.out.println("Sorted descending by fitness");

System.out.println("Element\t\tfitness");

for (int i=0; i><elemNameFromFile.size(); i++) {

prepGAGrammar g=gram;

ascElem.add(g.getCompoundElementName());

ascFitness.add(g.getElemFitness());

System.out.println(ascElem.get(i)+"\t"+ascFitness.get(i));

}//end for

}catch(IOException e){

e.printStackTrace();

}

}//end method

The output that I got is as follows:

IndexElement namecomponentsfitness

0elemG2-0placename streetname 0.159621785

1elemG2-1business streetname 0.166099364

2elemG2-2placename streetname [number] 0.47627142

3elemG2-3placename business 0.0383857

4elemG2-4streetending anyWord 0.15962179

I try to debug the code from the prepGAGrammar.java file. The compareTo method is as follows:

public int compareTo(Object anotherGrammar) throws ClassCastException {

/*if (!(anotherGrammar instanceof prepGAGrammar))

throw new ClassCastException("A Grammar object expected.");

int anotherGrammarElemFitness= (int)((prepGAGrammar) anotherGrammar ).getElemFitness();

return (int)this.elemFitness- anotherGrammarElemFitness;*/

prepGAGrammar g2= (prepGAGrammar)anotherGrammar; //We receive the object to compare

double f1 = elemFitness; //and we can access to the second object which is compared

double f2 = g2.elemFitness;

System.out.println("-");

System.out.println("f1:"+f1);

System.out.println("f2:"+f2);

//Here we decide which object must come first

if(f1><f2){

return -1;

}else if(f2>f1){

return 1;

}

return 0; //the freqs are equal

}//end method

The output from this is:

-

f1:0.159621785

f2:0.166099364

-

f1:0.159621785

f2:0.47627142

-

f1:0.166099364

f2:0.47627142

-

f1:0.159621785

f2:0.0383857

-

f1:0.0383857

f2:0.15962179

-

f1:0.159621785

f2:0.15962179

-

f1:0.166099364

f2:0.15962179

the errors that i got are:

Exception in thread "main" java.lang.NullPointerException

at prepGAGrammar.compareTo(prepGAGrammar.java:83)

at Descending.compare(Descending.java:9)

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

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

at doGA.sortElemByFitness(doGA.java:61)

at expGAMain.main(expGAMain.java:35)

How do I solve this?

help, please..Thank you in advance for your time and concern.

Cheers.

[4008 byte] By [brisJavaa] at [2007-11-27 10:04:28]
# 1
I did not read you code, but when you encounter a NPE while calling Arrays' sort(...) method, it means that your array contains one (or more) null references.So, make sure that your entire array is filled with objects.
prometheuzza at 2007-7-13 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 2
Rather the compareTo() method tries to access a null reference, fix that.
-Kayaman-a at 2007-7-13 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 3
And the next time you post some code, please use the code tags!Cheers,Manuel Leiria
manuel.leiriaa at 2007-7-13 0:39:28 > top of Java-index,Java Essentials,Java Programming...