title: Null pointer Exception in CompareTo method
body: <p>Structure of my class:</p>
  
  <pre><code>public class Priorityy implement Comparable {
      public int compareTo(Object pe) {
          Priorityy p = (Priorityy) pe;
          if (this.key &lt; p.key) {    
              return 1;
          } else if (this.key &gt; p.key) {
              return -1;
          } else {
              return 0;
          }
      }
  }
  </code></pre>
  
  <p>Th problem is that <code>p.key</code> is always null, why exactly is that? I have my array initialized with elements in it but it always throws NullPointerException whenever I try <code>Arrays.sort(arr)</code>.</p>
  
  <p>How can I fix this?</p>
  
  <p>Edit: Here is the complete code and print did print the elements of array <code>arr</code>:</p>
  
  <pre><code>import java.util.Arrays;
  
  class Priorityy implements Comparable {
      int size;
      int front = 0;
      int rear = 0;
      static Priorityy[] arr = new Priorityy[3];
      int key;
      String value;
  
      public Priorityy(int key, String value) {
          this.key = key;
          this.value = value;
          insert();
      }
  
      public void insert() {
          arr[front] = this;
          System.out.println(arr[front].value);
          while (front + 1 != 3) {
              front = front + 1;
          }
      }
  
      public Priorityy remove() {
          Priorityy x = arr[front];
          front = front - 1;
          return x;
      }
  
      public int compareTo(Object pe) {
          Priorityy p = (Priorityy) pe;
          if (this.key &lt; p.key) {
              System.out.println(p.key);
  
              return 1;
          } else if (this.key &gt; p.key) {
  
              System.out.println("3");
              return -1;
          } else {
              System.out.println("4");
              return 0;
          }
      }
  
      public static void main(String... s) {
          new Priorityy(10, "Watch");
          new Priorityy(40, "Laptop");
          new Priorityy(60, "Wallet");
          Arrays.sort(arr);
          for (Priorityy element : arr) {
              System.out.println(element.key);
              System.out.println(element.value);
          }
      }
  }
  </code></pre>