When try to add an object to my TreeSet, this exception pops up.
Exception in thread "main" java.lang.NullPointerException
at Circle.compareTo(Shape.java:47)
at Circle.compareTo(Shape.java:23)
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at CircleTreeSet.main(CircleTreeSet.java:24)
All I'm doing in my main method is creating the TreeSet, creating a Object, and adding it to the set.
Here is the main method:
class CircleTreeSet {
public static void main(String[] args) {
TreeSet<Circle> cs = new TreeSet<Circle>();
Circle circle1 = new Circle("circle1", 1);
cs.add(circle1);
}
}
Here is the class:
class Circle extends Shape implements Comparable<Circle> {
private static String name;
private int radius;
Circle(String n, int r) {
super(n);
radius = r;
}
public double area() {
return Math.PI * radius * radius;
}
public double perim() {
return 2 * Math.PI * radius;
}
public int compareTo(Circle c) {
return name.compareTo(c.name);
}
}