title: Java: When adding an object to a tree set exception gets thrown
body: <p>When try to add an object to my TreeSet, this exception pops up.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>All I'm doing in my main method is creating the TreeSet, creating a Object, and adding it to the set.</p>
  
  <p>Here is the main method:</p>
  
  <pre><code>class CircleTreeSet {
      public static void main(String[] args) {
          TreeSet&lt;Circle&gt; cs = new TreeSet&lt;Circle&gt;();
  
          Circle circle1 = new Circle("circle1", 1);
  
          cs.add(circle1);
      }
  }
  </code></pre>
  
  <p>Here is the class:</p>
  
  <pre><code>class Circle extends Shape implements Comparable&lt;Circle&gt; {
      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);
      }
  }
  </code></pre>