Results 1 to 11 of 11
- 11-07-2011, 04:37 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
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:
I then call an Arrays.sort() on an array of this object:Java 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; } ... }
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:Java Code:... Solution[] population; ... private Solution[] getFittest() { Solution[] solFittest = new Solution[intPopulationSize/2]; Random randomGenerator = new Random(); Arrays.sort(population); } ...
Thanks for any help in advance.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)
- 11-07-2011, 05:45 PM #2
Re: Problem implementing compareTo for Arrays.sort
Where do you add elements to the array: population? What is in it when you call sort?
- 11-07-2011, 05:51 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Re: Problem implementing compareTo for Arrays.sort
I have a generate population method which fills the array:
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.Java 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()); }
- 11-07-2011, 05:55 PM #4
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.
- 11-07-2011, 06:04 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
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).
- 11-07-2011, 06:36 PM #6
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.
- 11-07-2011, 06:52 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
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.
- 11-07-2011, 06:55 PM #8
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?
- 11-07-2011, 07:03 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
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.
- 11-07-2011, 07:05 PM #10
Re: Problem implementing compareTo for Arrays.sort
Your call to Arrays.toString() should have shown that the contents of the array was null???
- 11-07-2011, 07:08 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Implementing Sorting in Arrays Question
By jakaldama in forum New To JavaReplies: 2Last Post: 10-10-2011, 08:49 PM -
Need help in Designing and implementing bucket-sort algorithm
By wasiqjaved in forum New To JavaReplies: 0Last Post: 10-03-2011, 12:27 PM -
Implementing quick sort and binary search
By syle_q in forum New To JavaReplies: 1Last Post: 04-07-2011, 08:46 PM -
Sort with CompareTo
By ScienceLife in forum New To JavaReplies: 7Last Post: 12-01-2010, 01:08 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks