-1

Here, l have a quickSort that sorts an String array and where it starts and ends sortA(String[] s,int start, int end)

String[] wordlist = new String[5000000];
QuickSort quickSort = new QuickSort();
int number = 0, startN = 0;

 public void read() throws IOException
{
   Scanner keyboard = new Scanner(System.in);
   System.out.println("Please enter the name of the file you want to read from");
   String readFile = keyboard.nextLine();
   try
   {
     Scanner sc2 = null;
     sc2 = new Scanner(new File(readFile));
     startN = number;
     while (sc2.hasNextLine())
     {
        Scanner s2 = new Scanner(sc2.nextLine());
        boolean b;
        while (b = s2.hasNext())
        {
           String s = s2.next();
           s = s.replaceAll("[^A-Za-z]","");
           wordlist[number] = s;
           number++;
           System.out.println(s + "|" +number);
        }
     }
     quickSort.sortA(wordlist, startN, number);
   }
   catch(FileNotFoundException e)
   {
      System.out.println("File not Found, please try again");
   }
}

l am reading from a normal file and trying to sort, but it gives me :

java.lang.NullPointerException
 at java.lang.String.compareTo(String.java:578)
 at QuickSort.partition(QuickSort.java:21)
 at QuickSort.sortA(QuickSort.java:7)
 at WordMatch.read(WordMatch.java:33)
 at WordMatch.main(WordMatch.java:73)

going to QuickSort.java:

  public class QuickSort
  {
    static void sortA(String[] s, int start, int end)
    {
      if (end > start)
      {
       int pivot = partition(s, start, end);
       sortA(s, start, pivot-1);
       sortA(s, pivot, end);
  }
}

 private static int partition(String[] s, int start, int end)
 {
  String pivot = s[end];
  int left = start;
  int right = end;
  String temp = "";
  do
  {
     while ((s[left].compareTo(pivot) <= 0) && (left < end))
        left++;
     while ((s[right].compareTo(pivot) > 0) && (right > start))
        right--;
     if (left < right)
     {
        temp = s[left];
        s[left] = s[end];
        s[right] = temp;

     }
  } while (left < right);
  temp = s[left];
  s[left] = s[end];
  s[end] = temp;
  return left;
  }
}

But it doesn't seem wrong...

D-key Nguyen
  • 11
  • 1
  • 4
  • Maybe you could put a comment on the line that fails, to make it easier to help you, as the code doesn't contain line numbers – Ale Zalazar Jul 27 '14 at 22:54
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwin Bolwidt Jul 28 '14 at 02:11

2 Answers2

1

String.compareTo() will throw a NPE if passed a null, which is what you must be doing.

Looking at your code, you are passing s[end] (via the variable pivot), so your array must contain null(s). Tracing back through the call stack, we see that the array being passed down is wordList, which is initialized with a size of 5000000; unless you have exactly 5000000 words in your input, your code will throw an exception... 4999999 or less and you'll get a NPE, 5000001 or more and you'll get an ArrayIndexOutOfBoundsException.

The remedy is captured by one of my favourite mantras: Don't use arrays!

Use a List<String>, which grows in size as you need, to capture the input. If your algorithm needs an array, convert the list to an array via List's toArray() method, which returns an array that is exactly the right size. This change will fix your problem.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

It looks like problem with calling your quicksort quickSort.sortA(wordlist, startN, number);. Since before that line you have increased number in the loop, which means at the end number is having a increased value. you can try like this quickSort.sortA(wordlist, startN, number-1);

Thanks

Tapos
  • 601
  • 4
  • 8