Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

array.sort error: java.lang.nullPointerException.

807605Jul 9 2007 — edited Jul 9 2007
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:

Index Element name components fitness
0 elemG2-0 placename streetname 0.159621785
1 elemG2-1 business streetname 0.166099364
2 elemG2-2 placename streetname [number] 0.47627142
3 elemG2-3 placename business 0.0383857
4 elemG2-4 streetending 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 6 2007
Added on Jul 9 2007
3 comments
856 views