title: Merge sort with array lists in java
body: <p>So for homework I have to write a program that mergeSorts with array lists from a code that works with regular arrays, I was just wondering if someone could help me figure out where I went wrong, because my code throws a ton of NULL POINTER exceptions, and I have tried to fix them, but when I fix one it goes to another...and so on.... </p>
  
  <p>Thanks!
  My code:</p>
  
  <pre><code>private static ArrayList&lt;Integer&gt; numbers= new ArrayList&lt;Integer&gt;();
  private static ArrayList&lt;Integer&gt; helper;
  private static int number;
  public static void sort(ArrayList&lt;Integer&gt; myNumbers){
      for(int i=0; i&lt;myNumbers.size();i++){
          numbers.add(myNumbers.get(i));
      }
      //numbers=myNumbers;
      number = myNumbers.size()-1;
  
      mergesort(0, number -1);
  }
  private static void mergesort(int low, int high){
      //check if low is smaller than high, if not then the array is sorted
      if(low&lt;high){
          //get the index of the element which is in the middle
          int middle=low+(high-low)/2;
          //sort the left side of the array
          mergesort(low, middle);
          //sort the right side of the array
          mergesort(middle +1, high);
          //combine them both
          merge(low, middle, high);
      }
  }
  private static void merge(int low, int middle, int high){
      //copy both parts into the helper array
      for(int i=high;i&gt;low;i++){
          helper.add((numbers.get(i)));
      }
  
      int i=low;
      int j=middle+1;
      int k=low;
      //copy the smallest myNumbers from either the left or right side back to the original array
      while(i&lt;middle  &amp;&amp; j&lt;high){
          if(helper.get(i)&lt; helper.get(j)){
              numbers.set(k,(helper.get(i)));
              i++;
          }
          else{
              numbers.set(k,(helper.get(j)));
              j++;
          }
          k++;
      }
      //copy the rest of the left side of the array into target array
      while(i&lt;middle){
          numbers.set(k,helper.get(i));
          k++;
          i++;
      }
  }
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at BinarySearch.merge(BinarySearch.java:61)
  at BinarySearch.mergesort(BinarySearch.java:55)
  at BinarySearch.mergesort(BinarySearch.java:51)
  at BinarySearch.mergesort(BinarySearch.java:51)
  at BinarySearch.mergesort(BinarySearch.java:51)
  at BinarySearch.mergesort(BinarySearch.java:51)
  at BinarySearch.mergesort(BinarySearch.java:51)
  at BinarySearch.mergesort(BinarySearch.java:51)
  at BinarySearch.sort(BinarySearch.java:43)
  at BinarySearch.main(BinarySearch.java:25)
  </code></pre>