-2

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:

>  Exception in thread "main" java.lang.NullPointerException    
>         at Distance.compareTo(Distance.java:28)
>       at Distance.compareTo(Distance.java:1)
>       at Driver05.findMax(Driver05.java:54)
>       at Driver05.sort(Driver05.java:43)
>       at Driver05.input(Driver05.java:26)
>       at Driver05.main(Driver05.java:7)

What is this error and What can I do to fix it?

The File that makes data(generates data randomly in feet and inches):

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 < numitems; k++)
         {
            System.out.println((int)(Math.random() * 100));
            System.out.println((int)(Math.random() * 12));
         }
      }
   }

The Distance Class:

public class Distance implements Comparable<Distance> {
   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. ";
   }
}

The Distance class serves the purpose of allowing me to more easily compare distances.

The Actual Driver:

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 < 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 < array.length; k++)
            System.out.println(array[k].toString());
      }

       public static void sort(Comparable[] array)
      {
         int maxPos;
         for(int k = 0; k < 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 < endIndex; x++) {

                  if (enterArray[max].compareTo(enterArray[x]) < 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;
        }

   }

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

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

1 Answers1

0

Inside your sort method the for loop starts with k=0 which passes the endIndex in findMax as array.length. But valid last index of array should be array.length-1.

Do the following change :

for(int k = 1; k <= array.length; k++)
         {
            maxPos = findMax(array, array.length - k);
            swap(array, maxPos, array.length - k - 1);
         }
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62