Results 1 to 11 of 11
  1. #1
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default 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:

    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;
        }
    
        ...
    }
    I then call an Arrays.sort() on an array of this object:

    Java 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:

    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: Problem implementing compareTo for Arrays.sort

    Where do you add elements to the array: population? What is in it when you call sort?

  3. #3
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default Re: Problem implementing compareTo for Arrays.sort

    I have a generate population method which fills the array:

    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());
        }
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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.

  5. #5
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default 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).

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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.

  7. #7
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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?

  9. #9
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: Problem implementing compareTo for Arrays.sort

    Your call to Arrays.toString() should have shown that the contents of the array was null???

  11. #11
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default 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.

Similar Threads

  1. Implementing Sorting in Arrays Question
    By jakaldama in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 08:49 PM
  2. Replies: 0
    Last Post: 10-03-2011, 12:27 PM
  3. Implementing quick sort and binary search
    By syle_q in forum New To Java
    Replies: 1
    Last Post: 04-07-2011, 08:46 PM
  4. Sort with CompareTo
    By ScienceLife in forum New To Java
    Replies: 7
    Last Post: 12-01-2010, 01:08 PM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •