title: Finding elements in array with binary search
body: <p>Trying to use <code>Arrays.binarySearch()</code> to search for a string in an array and return the index. However each time I call <code>Arrays.binarySearch()</code> I get the following exception - </p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at java.util.Arrays.binarySearch0(Unknown Source)
  at java.util.Arrays.binarySearch(Unknown Source)
  at project.ArrayDirectory.lookupNumber(ArrayDirectory.java:97)
  at project.test.main(test.java:12)
  </code></pre>
  
  <p>Here is my <code>ArrayDirectory class</code> -</p>
  
  <pre><code>    public class ArrayDirectory implements Directory {
  
  static Entry[] directory = new Entry[50];
  
  @Override
  public void addEntry(String surname, String initials, int extension) {
  
      int n = 0;
      for (int i = 0; i &lt; directory.length; i++) { // counting number of
                                                      // entries in array
          if (directory[i] != null) {
              n++;
          }
  
      }
  
      if (n == directory.length) {
          Entry[] temp = new Entry[directory.length * 2]; // if array is full
                                                          // double the
                                                          // length.
  
          for (int i = 0; i &lt; directory.length; i++)
              temp[i] = directory[i];
          directory = temp;
      }
  
      int position = -1;
      for (int i = 0; i &lt; directory.length; i++) {
  
          position = i;
  
          if (directory[i] != null) { // sorting the array into alphabetical
                                      // order by surname.
  
              int y = directory[i].getSurname().compareTo(surname);
              if (y &gt; 0) {
                  break;
              }
          }
  
          else if (directory[i] == null) {
              break;
          }
  
      }
  
      System.arraycopy(directory, position, directory, position + 1,
              directory.length - position - 1);
  
      directory[position] = new Entry(initials, surname, extension); // placing
                                                                      // new
                                                                      // entry
                                                                      // in
                                                                      // correct
                                                                      // position.
  
  }
  
  @Override
  public int lookupNumber(String surname, String initials) {
      // TODO Auto-generated method stub
  
      Entry lookup = new Entry(surname, initials);
      int index = Arrays.binarySearch(directory, lookup);
      return index;
  }
  </code></pre>
  
  <p>}
  Any ideas how I use binary search to find the correct index?</p>
  
  <p>Thank you for you help.</p>
  
  <p>edit - </p>
  
  <p>I have also overridden <code>comapreTo</code>in my <code>Entry</code> class -</p>
  
  <pre><code>public int compareTo(Entry other) {
      return this.surname.compareTo(other.getSurname());
  }
  </code></pre>