Exception in thread "main" java.lang.NullPointerException while performing selection sort? : <p>I was writing some code to create a selection sort algorithm that sorted objects of the distance class. However, when I ran the program, the program returned the error:</p>

<pre><code>&gt;  Exception in thread "main" java.lang.NullPointerException    
&gt;         at Distance.compareTo(Distance.java:28)
&gt;       at Distance.compareTo(Distance.java:1)
&gt;       at Driver05.findMax(Driver05.java:54)
&gt;       at Driver05.sort(Driver05.java:43)
&gt;       at Driver05.input(Driver05.java:26)
&gt;       at Driver05.main(Driver05.java:7)
</code></pre>

<p>What is this error and What can I do to fix it?</p>

<p>The File that makes data(generates data randomly in feet and inches):</p>

<pre><code>import java.io.*;
   public class MakeDataFile
   {
      public static void main(String[] args) throws Exception
      {
         System.setOut(new PrintStream(new FileOutputStream("data.txt")));
         int numitems = (int)(Math.random() * 25 + 50);
         System.out.println(numitems);
         for(int k = 0; k &lt; numitems; k++)
         {
            System.out.println((int)(Math.random() * 100));
            System.out.println((int)(Math.random() * 12));
         }
      }
   }
</code></pre>

<p>The Distance Class: </p>

<pre><code>public class Distance implements Comparable&lt;Distance&gt; {
   private int myFeet, myInches;
   public Distance() {
      myFeet = myInches = 0;
      }
   public Distance(int x, int y) {
      myFeet = x;
      myInches = y;
   }

   public int getFeet() {
      return myFeet;
   }
   public int getInches() {
      return myInches;
   }

   public void setFeet(int x) {
      myFeet = x;
   }

   public void setInches(int x) {
      myInches = x;

   }
   public int compareTo( Distance d) {
      int myTotal = myFeet * 12 + myInches;
      int dTotal = d.getFeet() * 12 + d.getInches();
      return myTotal - dTotal;
         }
   public boolean equals(Distance arg) {
      return compareTo(arg) == 0;
   }
   public String toString()
   {
   return myFeet + " ft. " + myInches + " in. ";
   }
}
</code></pre>

<p>The Distance class serves the purpose of allowing me to more easily compare distances. </p>

<p>The Actual Driver:</p>

<pre><code>import java.io.*;      //the File class
   import java.util.*;    //the Scanner class
    public class Driver05
   {
       public static void main(String[] args) throws Exception
      {
         Comparable[] array = input("data.txt");
         sort(array);
         output(array, "output.txt");
      }

       public static Comparable[] input(String filename) throws Exception
      { 
         Scanner infile = new Scanner( new File(filename) );
         int numitems = infile.nextInt();
         Comparable[] array = new Distance[numitems];
         int x = 0;
         for(int k = 1; k &lt; numitems; k = k+2)
         {
            int feetNum = infile.nextInt();
            int inchNum = infile.nextInt();
            array[x] = new Distance(feetNum, inchNum);
         }
         infile.close();

         sort(array);
         output(array, "output.txt");
         return array;
      }

       public static void output(Object[]array, String filename) throws Exception
      {
         System.setOut(new PrintStream(new FileOutputStream(filename)));
         for(int k = 0; k &lt; array.length; k++)
            System.out.println(array[k].toString());
      }

       public static void sort(Comparable[] array)
      {
         int maxPos;
         for(int k = 0; k &lt; array.length; k++)
         {
            maxPos = findMax(array, array.length - k);
            swap(array, maxPos, array.length - k - 1);
         }
      }

            private static int findMax(Comparable[] enterArray, int endIndex) {
               int max = 0;


               for(int x = 0; x &lt; endIndex; x++) {

                  if (enterArray[max].compareTo(enterArray[x]) &lt; 0) {
                     max = x;
                  }

                  }
                  return max;
               }

        private static void swap(Comparable[] enterArray,int maxIndex,int lastIndex) {
            Comparable temp;
            temp = enterArray[maxIndex];
            enterArray[maxIndex] = enterArray[lastIndex];
            enterArray[lastIndex] = temp;
        }

   }
</code></pre>

<p>The driver contains the selection sort algorithm that sorts the data obtained from the output of the MakeData file. </p>

<p>It would be very helpful if someone could explain this error and how to correct it. </p>
