Problem implementing compareTo for Arrays.sort
Hi,
I'm trying to set up an array sort for some of my objects.
I've implemented the compareTo method in my object class:
Code:
public class Solution implements Comparable<Solution>
{
Artist[] arrArtists = new Artist[4];
FitnessAnalyser fitAnalyser;
...
/* Overload compareTo method */
public int compareTo(Solution obj)
{
Solution tmp = (Solution)obj;
if(this.getFitness() < tmp.getFitness())
{
return -1;
}
else if (this.getFitness() > tmp.getFitness())
{
return 1;
}
return 0;
}
...
}
I then call an Arrays.sort() on an array of this object:
Code:
...
Solution[] population;
...
private Solution[] getFittest()
{
Solution[] solFittest = new Solution[intPopulationSize/2];
Random randomGenerator = new Random();
Arrays.sort(population);
}
...
When I run this code, I get an error returned when it tries to do the sort. I've been googling and checking the forums and I can't figure this one out:
Quote:
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.countRunAndMakeAscendi ng(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at cs39440.ttb8.FineTune.SolutionGenerator.getFittest (SolutionGenerator.java:116)
at cs39440.ttb8.FineTune.SolutionGenerator.createNext Generation(SolutionGenerator.java:104)
at cs39440.ttb8.FineTune.SolutionGenerator.generate(S olutionGenerator.java:93)
at cs39440.ttb8.FineTune.Main.main(Main.java:17)
Thanks for any help in advance.
Re: Problem implementing compareTo for Arrays.sort
Where do you add elements to the array: population? What is in it when you call sort?
Re: Problem implementing compareTo for Arrays.sort
I have a generate population method which fills the array:
Code:
private void generatePopulation()
{
population = new Solution[intPopulationSize];
Random randomGenerator = new Random();
for(int i = 0; i < intPopulationSize; i++)
{
Artist[] arrArtistsTemp = new Artist[4];
for(int j = 0; j < arrArtistsTemp.length; j++)
{
int randomInt;
do
{
randomInt = randomGenerator.nextInt(arrArtistData.size());
}while(exists(arrArtistsTemp,randomInt));
arrArtistsTemp[j] = arrArtistData.get(randomInt);
}
Solution solTemp = new Solution(arrArtistsTemp, fitAnalyser);
population[i] = solTemp;
}
randomGenerator.nextInt(arrArtistData.size());
}
The above method (generatePopulation) and the getFittest() method you can see in my first post are in a class called SolutionGenerator. I call generatePopulation and then getFittest.
Re: Problem implementing compareTo for Arrays.sort
Print out the what is in the array before and after the call to sort() to see what is happening.
Add a toString() method to the Solution class that returns a String representing the object's contents.
Use the Arrays.toString() method to print out the array.
Re: Problem implementing compareTo for Arrays.sort
I already had a toString method implemented, so I did as you suggested and printed Arrays.toString(population) before the sort. The output was as expected. All artists contained in each Solution object.
I can't print after the sort as when it comes to execute the sort I get the error stated in my first post.
It's probably worth mentioning that the "getFitness" method used in the sort method uses a random number generator at the moment, so they'd be in a random order anyway. But I just don't understand why it's crashing. (I've yet to implement my getFitness method and just want to get this section working first).
Re: Problem implementing compareTo for Arrays.sort
What I don't understand is the stack trace you posted in the error message shows that the last of your code to be executed was the call to the sort() method. There is no reference to code being executed in the compareTo() method.
Add a println to the compareTo() method to see if it is being called.
Re: Problem implementing compareTo for Arrays.sort
Ah. It's after the compareTo method. I added a print statement at the start of the compare and it ran 114 times before the error. So I imagine it's an issue with something the sort calls after the compare method.
Re: Problem implementing compareTo for Arrays.sort
And the error message still does not show your code as the location of the NullPointerException?
Can you strip your code down to a minimum that still gets the error and post it here for others to look at?
Re: Problem implementing compareTo for Arrays.sort
I think I have it.
I didn't realise it was the second call of the sort method that was failing. It's in a loop.
It passes the array as all nulls to the sort the second time. So I need to find out why my code nulls the array the second time round.
Re: Problem implementing compareTo for Arrays.sort
Your call to Arrays.toString() should have shown that the contents of the array was null???
Re: Problem implementing compareTo for Arrays.sort
The first time it wasn't null, the second it was. And yes it did show nulls on the second time round the loop, I failed to notice the null print out, I only saw the first print.