Results 1 to 18 of 18
Thread: Comparator Tree Set with Strings
- 02-17-2011, 01:36 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.
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: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!**** } }
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!****
- 02-17-2011, 01:41 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens if you print the info in a vehicle? The constructor looks like it takes 3 arguments but does not set them.
-
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?
- 02-17-2011, 01:44 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 02-17-2011, 01:59 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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)
I do not have anything set to Null....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"); ***
-
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.I do not have anything set to Null....
look at the code for your constructor (again).Last edited by Fubarable; 02-17-2011 at 02:21 AM.
- 02-17-2011, 02:25 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
I thought this was why.....
Now, I thought I initialized / implemented the vehicles with the following top to the Comparator code (seen below)
Do I really need to set up the setters and getters in the Sedan and Truck and Motorcycle classes?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"); ***
- 02-17-2011, 02:31 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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....
-
Again
Please look at your C. O. N. S. T. R. U. C. T. O. R.
Again, you're throwing away the parameters. Where do you set the class's color, speed, and nickname fields???Java Code:public Vehicle(String color, double speed, String nickname) { }
-
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 } }
- 02-17-2011, 02:42 AM #11
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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:
The output is shown as :Java Code:[Sedan@1cc2ea3f] [Sedan@1cc2ea3f] [Motorcycle@40a0dcd9, Sedan@1cc2ea3f]
Thanks for helping with this, seriously. You guys help me figure this out quick!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); **
-
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.
- 02-17-2011, 02:58 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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?
- 02-17-2011, 03:12 AM #14
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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...*** } }
- 02-17-2011, 03:14 AM #15
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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...**
- 02-17-2011, 03:50 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 02-17-2011, 03:51 AM #17
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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!
- 02-17-2011, 03:58 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
All you needed was to define the constructor to initialize the variables
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 likeJava Code:public Vehicle(String color, double speed, String nickname) { this.color = color; this.speed = speed; this.nickname = nickname; }
Then you would instantiate them with the correct constructor, and they would inherit everything from the abstract class.Java Code:class Sedan extends Vehicle class Motorcycle extends Vehicle
You could have done something like
The idea of the comparators were supposed to be designed like soJava Code:class Sedan extends Vehicle{ Sedan(String color, String speed, String nickname){ super(color, speed, nickname); } }
The other two comparators would have been nearly identical.Java Code:class MyColorComparator implements Comparator<Vehicle>, Serializable{ public int compare(Vehicle v1, Vehicle v2){ return v1.color.compareTo(v2.color); } }Last edited by sunde887; 02-17-2011 at 04:04 AM.
Similar Threads
-
how to use comparator
By KidneyinaCooler in forum Advanced JavaReplies: 2Last Post: 07-18-2010, 10:25 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Use different comparator for SortedSet
By linus_k in forum New To JavaReplies: 0Last Post: 11-21-2008, 02:46 PM -
How to search with a Comparator
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM -
How to write your own Comparator
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks