Results 1 to 18 of 18
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Comparator Tree Set with Strings

    I have posted alot this weekend about this program, so I am sorry for the constant harrasment. I will post the code and the driver for my Tree Set. I will then post the error message.

    Java Code:
    import java.util.Comparator;
    
    public class VehicleColorComparator 
    implements Comparator<Vehicle>, java.io.Serializable 
    
      {
    	
    	// uses the inherited compare method for the colors
    public int compare (Vehicle o1, Vehicle o2) 
    {
    	
    	String Color1 = o1.color;
    	String Color2 = o2.color;
       
    		
         return Color1.compareTo(Color2);  ** this is the problem!****
    
     }
    
    }
    That is the comparator that compares my abstract Vehicle's colors. For any clarification, here is the Vehicle class itself. This is according to the homework specifications and this is correct.

    Java Code:
    
    public abstract class Vehicle
    
    {
    	String color;
    	double speed;
                 String nickname;
    	
    	
    	public Vehicle(String color, double speed, String nickname)
    	{
    		
    	}
    	
    	
    	
    	public abstract double getNumberOfWheels();
    
    }

    Lastly, here is the Driver class, for running the actual program. This is the problem area, I believe if the ColorComparator class is not messing up.


    Java Code:
    
    import java.awt.Color;
    import java.io.Serializable;
    import java.util.Comparator;
    import java.util.TreeSet;
    
    class MyComparator implements Comparator,Serializable 
    {
    	
                  static Sedan s1 = new Sedan("BLUE", 112, "Buggy");
    	static Motorcycle s2 = new Motorcycle("RED", 120, "HOTRIDE");
    	static Truck s3 = new Truck("BLACK", 65, "HEARSE");
    
     
      // main method that sets up the TreeSets.
    public static void main(String args[]) 
    {
           
        TreeSet<Vehicle> comp1 = new TreeSet<Vehicle>(new VehicleColorComparator()); // uses for ColorComparator
        
        comp1.add(s1);
        comp1.add(s2); // this pulls the error. When I "//" it out, then
        comp1.add(s3); // this then is the error. 
        
       
       TreeSet<Vehicle> comp2 = new TreeSet<Vehicle>(new VehicleSpeedComparator()); // uses for SpeedComparator
        
        comp2.add(s1);
        comp2.add(s2);
        comp2.add(s3);
        
    
       TreeSet<Vehicle> comp3 = new TreeSet<Vehicle>(new VehicleWheelComparator()); // uses for WheelComparator
        
        comp3.add(s1);
        comp3.add(s2);
        comp3.add(s3);
        
        
        
      
        //  System.out.println(ts);
          System.out.println(comp1);
          System.out.println(comp2);
          System.out.println(comp3);
    
        
      }
    
    
    
    @Override
    public int compare(Object o1, Object o2) {
    	// TODO Auto-generated method stub
    	return 0;
    }
    
    
    }


    ERROR MESSAGE IS:

    Exception in thread "main" java.lang.NullPointerException
    at VehicleColorComparator.compare(VehicleColorCompara tor.java:27)
    at VehicleColorComparator.compare(VehicleColorCompara tor.java:1)
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)
    at MyComparator.main(MyComparator.java:29)


    The error at this program is found at ColorComparator class, which I commented:


    return Color1.compareTo(Color2); ** this is the problem!****

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you print the info in a vehicle? The constructor looks like it takes 3 arguments but does not set them.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The problem isn't in the line you've indicated, but it hints at what the problem is. The real problem is that your color Strings are null. To find out why, you'd better check to see where/if you initialize them. For instance, I see you throwing out the parameters passed into the Vehicle constructor -- you never use them -- why?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When testing your code make sure to check out what exception is thrown, many times it will give you the answer. In this case it's a NullPointerException, perhaps you have something that is null?

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I was told per professors instructions to do the following:



    1) Define an abstract "Vehicle" class (it does not implement the interface Comparator.
    2) Define concrete classes “Sedan”, “Motorcycle”, and “Truck” as subclasses of “Vehicle”. They do not implement the interface Comparator either.
    3) Define three comparators. They all implement the interface Comparator and java.io.Serializable. One defines the method "compare" to compare two Vehicle objects based on speed, one defines the method "compare" to compare two Vehicle objects based on color, and one defines the method "compare" to compare two Vehicle objects based on the number of wheels.
    4) Define a driver class. In its Main method, define and instantiate objects of Sedan, Motorcycle, and Truck. Define three TreeSet objects, and each of them holds vehicles using one of the comparators defined in step 3)



    Now, I thought I initialized / implemented the vehicles with the following top to the Comparator code (seen below)

    Java Code:
    
    import java.awt.Color;
    import java.io.Serializable;
    import java.util.Comparator;
    import java.util.TreeSet;
    
    class MyComparator implements Comparator,Serializable 
    {
    	
        static Sedan s1 = new Sedan("BLUE", 112, "Buggy");           ** here**
       static Motorcycle s2 = new Motorcycle("RED", 120, "HOTRIDE");  **
       static Truck s3 = new Truck("BLACK", 65, "HEARSE");                ***
    I do not have anything set to Null....

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I do not have anything set to Null....
    you don't have to set anything to null for it to be null. you just need a field that you never set to an object.

    look at the code for your constructor (again).
    Last edited by Fubarable; 02-17-2011 at 02:21 AM.

  7. #7
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The problem isn't in the line you've indicated, but it hints at what the problem is. The real problem is that your color Strings are null. To find out why, you'd better check to see where/if you initialize them. For instance, I see you throwing out the parameters passed into the Vehicle constructor -- you never use them -- why?
    I thought this was why.....

    Now, I thought I initialized / implemented the vehicles with the following top to the Comparator code (seen below)


    Java Code:
         
    import java.awt.Color;
    import java.io.Serializable;
    import java.util.Comparator;
    import java.util.TreeSet;
    
    class MyComparator implements Comparator,Serializable 
    {
    	
        static Sedan s1 = new Sedan("BLUE", 112, "Buggy");           ** here**
       static Motorcycle s2 = new Motorcycle("RED", 120, "HOTRIDE");  **
       static Truck s3 = new Truck("BLACK", 65, "HEARSE");                ***
    Do I really need to set up the setters and getters in the Sedan and Truck and Motorcycle classes?

  8. #8
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I tried to put in all of the setters and getters for each class (even Vehicle). I still get the same Color error as before.

    You say it is in the constructors, but I am walking blind in broad daylight. I am sorry guys....

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again

    Please look at your C. O. N. S. T. R. U. C. T. O. R.

    Java Code:
    	public Vehicle(String color, double speed, String nickname)
    	{
    		
    	}
    Again, you're throwing away the parameters. Where do you set the class's color, speed, and nickname fields???

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you have a class with a field, say a String called foo:

    Java Code:
    class MyClass {
       String foo;
    
    }

    foo is null unless it is initialized to something. If you are going to be passing a String in the constructor parameter, you should use it to set your field.

    You do this:


    Java Code:
    class MyClass {
       String foo;
    
       public MyClass(String foo) {
    
       }
    
    }

    But that does nothing with the field of the class which is still null. You need to set the field with the parameter --


    Java Code:
    class MyClass {
       String foo;
    
       public MyClass(String foo) {
          this.foo = foo; // makes the field non-null
       }
    
    }

  11. #11
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I just got it running to where there is no error.

    I was just doing that when you replied. (Darn)

    The only output is the memory location - when I wanted the treeset organized for speed and color, etc. On my output it shows:

    Java Code:
    [Sedan@1cc2ea3f]
    [Sedan@1cc2ea3f]
    [Motorcycle@40a0dcd9, Sedan@1cc2ea3f]
    The output is shown as :

    Java Code:
      TreeSet<Vehicle> comp1 = new TreeSet<Vehicle>(new VehicleColorComparator()); // uses for ColorComparator
        
        comp1.add(s1);
        comp1.add(s2);
       comp1.add(s3);
        
       
       TreeSet<Vehicle> comp2 = new TreeSet<Vehicle>(new VehicleSpeedComparator()); // uses for SpeedComparator
        
        comp2.add(s1);
        comp2.add(s2);
        comp2.add(s3);
        
    
       TreeSet<Vehicle> comp3 = new TreeSet<Vehicle>(new VehicleWheelComparator()); // uses for WheelComparator
        
        comp3.add(s1);
        comp3.add(s2);
        comp3.add(s3);
        
        
        
      
          System.out.println(comp1);   ** ? **   
          System.out.println(comp2);     **
          System.out.println(comp3);  **
    Thanks for helping with this, seriously. You guys help me figure this out quick!

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You need to override public String toString() for the Vehicle class. Have it return a String that shows all the pertinent data held in the fields. Also on an unrelated note, there's no reason that your driver class would implement Comparator or Serializable.

  13. #13
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You need to override public String toString() for the Vehicle class. Have it return a String that shows all the pertinent data held in the fields. Also on an unrelated note, there's no reason that your driver class would implement Comparator or Serializable.
    Can I do that on an abstract class? It was not in the assignment, hence why I ask.

    Usually with Eclipse, it would automatically show/materialize the overridden method in the code - so while I can create the method, it would not be "overridden" unless I did something else with the code, right?

  14. #14
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I did an override anyways to return a string for the color. Now it gives me the same error as before.

    here is the code for my vehicle class as is:

    Java Code:
    public abstract class Vehicle
    
    {
    	String color;   **** when I initialize this, to "BLACK" for example,
    	double speed;   **** then all it outputs is "BLACK" Isnt this supposed 
                String nickname;      ** to be the abstract class?
    	
    	
    	public Vehicle(String color, double speed, String nickname)
    	{
    		
    	}
    	
    
    	
    	public String setNickname(String FourWheel)
    	{
    		return this.getType();
    	}
    	
    	public String getType()
    	{
    		String FourWheel = "FOUR";
    		return FourWheel;
    	}
    	
    	
    	
    	
    
    	public String getColor()
    	{
    		return color;
    	}
    	
    	public String setColor()
    	{
    		return this.getColor();
    	}
    	
    	public double getSpeed()
    	{
    		return speed;
    	}
    	
    	public double setSpeed()
    	{
    		return this.getSpeed();
    	}
    	
    	
    	
    	
    	
    	public abstract double getNumberOfWheels();
    	
    	
    	@Override public String toString() { 
    	    return color;  ** I was testing this only first...***
    	  }
    
    
    
    }

  15. #15
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Here is the same error code as before:


    Exception in thread "main" java.lang.NullPointerException
    at VehicleColorComparator.compare(VehicleColorCompara tor.java:27)
    at VehicleColorComparator.compare(VehicleColorCompara tor.java:1)
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)
    at MyComparator.main(MyComparator.java:29)
    ** same line of comparator...**

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't want to seem rude(or maybe there is code you are not showing us) but look at the exception, NullPointerExceptions, you have not made a correct constructor

    CONSTRUCTOR

    Create it to initialize the variables and you won't get a null pointer exception.

  17. #17
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Its ok all, I just turned it in as is.

    I can accept the F. His assignment really threw me in a loop! (Pun intended)

    Thanks Furbable for all the help. You guys are the best!

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    All you needed was to define the constructor to initialize the variables

    Java Code:
    public Vehicle(String color, double speed, String nickname)
    	{
                this.color = color;
                this.speed = speed;
                this.nickname = nickname;		
    	}
    However this was an abstract class which means it should never be instantiated. I am not sure if you didn't show us all your code but, you should have had stuff like
    Java Code:
    class Sedan extends Vehicle
    
    class Motorcycle extends Vehicle
    Then you would instantiate them with the correct constructor, and they would inherit everything from the abstract class.

    You could have done something like
    Java Code:
    class Sedan extends Vehicle{
      Sedan(String color, String speed, String nickname){
        super(color, speed, nickname);
      }
    }
    The idea of the comparators were supposed to be designed like so
    Java Code:
    class MyColorComparator implements Comparator<Vehicle>, Serializable{
      public int compare(Vehicle v1, Vehicle v2){
        return v1.color.compareTo(v2.color);
      }
    }
    The other two comparators would have been nearly identical.
    Last edited by sunde887; 02-17-2011 at 04:04 AM.

Similar Threads

  1. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 10:25 AM
  2. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  3. Use different comparator for SortedSet
    By linus_k in forum New To Java
    Replies: 0
    Last Post: 11-21-2008, 02:46 PM
  4. How to search with a Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM
  5. How to write your own Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:38 PM

Posting Permissions

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