I have a lab that requires me to sort type T arrays with assertion sort and merge sort. But my merge sort is giving me some problems and I'm not sure why. Thank you in advance for any help!
ArraySort.java
/** * @param <T> * */ public class ArraySort<T> { /** * @param a * @param length */ public static<T extends Comparable<T>> void insertionSort(T[] a, int length){ int j; T temp; for(int i=1;i<length;i++){ temp = a[i]; j=i; while(j>0 && (a[j-1].compareTo(temp)>0)) { a[j] = a[j-1]; j--; } a[j] = temp; } } /** * @param a * @param length */ @SuppressWarnings("javadoc") public static<T extends Comparable<T>> void mergeSort(T[] a, int length){ SortMerge(a, length, 0, length-1); } /** * @param a * @param length * @param first * @param last */ @SuppressWarnings("javadoc") private static<T extends Comparable<T>> void SortMerge(T[] a, int length, int first, int last ){ if(first < last){ int middle = first + (last-first)/2; SortMerge(a, length, first, middle); SortMerge(a, length, middle+1, last); merge(a, length, first, middle, last); } } /** * @param a * @param length * @param first * @param middle * @param last */ @SuppressWarnings({ "null", "javadoc" }) private static<T extends Comparable<T>> void merge(T[] a, int length, int first, int middle, int last){ int i = first; int j = middle + 1; int k = first; T[] temp = null; for (int x = first; x < length; x++){ temp[x] = a[x]; } while(i <= middle && j <= last){ if(temp[i].compareTo(temp[j]) > 0){ a[k] = temp[i]; i++; } else{ a[k] = temp[k]; j++; } k++; } while(i <= middle){ a[k]=temp[i]; k++; i++; } } }
and the driver just in case you needed it
SortDriver.java
and the output
Exception in thread "main" java.lang.NullPointerException at ArraySort.merge(ArraySort.java:69) at ArraySort.SortMerge(ArraySort.java:51) at ArraySort.SortMerge(ArraySort.java:49) at ArraySort.SortMerge(ArraySort.java:49) at ArraySort.SortMerge(ArraySort.java:49) at ArraySort.mergeSort(ArraySort.java:35) at SortDriver.main(SortDriver.java:15)