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....
Thanks! My code:
private static ArrayList<Integer> numbers= new ArrayList<Integer>();
private static ArrayList<Integer> helper;
private static int number;
public static void sort(ArrayList<Integer> myNumbers){
for(int i=0; i<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<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>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<middle && j<high){
if(helper.get(i)< 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<middle){
numbers.set(k,helper.get(i));
k++;
i++;
}
}
Returns:
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)
helperis not initialized. That's causing you trouble