title: Sorting an array of strings in Java
body: <p>The user is allowed to play with an array of strings. They can add strings to the array, remove strings from the array, search for strings in the array, and eventually they will be able to sort the array. The sorting is what is messing me up. I've tried a few different approaches. The first approach was to convert the array into an ArrayList and use Collections to sort the ArrayList, which would be converted back into the static class array. It doesn't work. The second approach I tried was to iterate through the array and try to sort only the strings added by the user instead of everything in the array (since there are some null values in the array). Perhaps I should iterate through the array and then store the non-null values into a new array that I can then sort? But what if I want to add more strings after sorting the new array? That's why I stopped with the second solution. The third attempt was to use Arrays.sort() on my array but for some reason it does not work. </p>
  
  <p>Here is the exception:</p>
  
  <pre><code> Exception in thread "main" java.lang.NullPointerException 
      at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290) 
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:157) 
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:146) 
      at java.util.Arrays.sort(Arrays.java:472) 
      at java.util.Collections.sort(Collections.java:155) 
      at testingSearch.sortArray(testingSearch.java:93) 
      at testingSearch.main(testingSearch.java:42) 
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Arrays;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  
  public class testingSearch {
  
      static String[] strArray;
      static {
          strArray = new String[5];
      }
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
  
          while(true){
              System.out.println("1. Add string to the string array.");
              System.out.println("2. Remove string from the string array.");
              System.out.println("3. Display strings in string array.");
              System.out.println("4. Search the string array for a string.");
              System.out.println("5. Sort the strings in the string array.");
  
              int userChoice = 0;
              userChoice = input.nextInt();
  
              switch(userChoice) {
              case 1:
                  addString();
                  break;
              case 2:
                  removeString();
                  break;
              case 3:
                  displayStrings();
                  break;
              case 4:
                  searchArray();
                  break;
              case 5:
                  sortArray();
                  break;
              }
          }
  
      }
  
      public static void addString(){
          Scanner input = new Scanner(System.in);
          System.out.println("What string do you want to add?");
          String userInput;
          userInput = input.nextLine();
                  ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt; (Arrays.asList(strArray));
          stringList.add(userInput);
          strArray = stringList.toArray(strArray);
      }
  
      public static void removeString(){
          Scanner input = new Scanner(System.in);
          System.out.println("What string do you want to remove?");
          String userInput;
          userInput = input.nextLine();
          ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt;    (Arrays.asList(strArray));
          stringList.remove(userInput);
          strArray = stringList.toArray(strArray);
      }
  
      public static void displayStrings(){
          for (String s: strArray){
              if (!(s == null)){
                  System.out.println(s);
              }
          }
      }
  
      public static void searchArray(){
          Scanner input = new Scanner(System.in);
          System.out.println("What string do you want to search the array for?");
          String userInput;
          userInput = input.nextLine();
          ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt;(Arrays.asList(strArray));
          if (stringList.contains(userInput)){
              System.out.println("The string array contains that string!");
          }
          else {
              System.out.println("The string array does not contain that string...");
          }
      }
  
      public static void sortArray(){
          /*ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt; (Arrays.asList(strArray));
          Collections.sort(stringList);
          strArray = stringList.toArray(strArray);*/
  
          /*for (String s: strArray) {
              if (!(s == null)){
                  Arrays.sort(strArray);
              }
          }*/
  
          List&lt;String&gt; stringList = new ArrayList&lt;String&gt;(Arrays.asList(strArray));
          Collections.sort(stringList);
          strArray = stringList.toArray(strArray);
  
          //Arrays.sort(strArray);
  
      }
  
  }
  </code></pre>