I have a generic class in my Java program, and the type can either be Double or Integer. I need to write a function that first needs to sort an array of values of the generic type. I am getting the following exception, and I am unsure how to fix it. I would appreciate any help.
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 datastructures.CircularBuffer.getMedian(CircularBuffer.java:98)
at test.CircularBufferTests.test4(CircularBufferTests.java:67)
at test.CircularBufferTests.main(CircularBufferTests.java:13)
Here is my code:
public class CircularBuffer<T extends Number> {
private T[] array;
public CircularBuffer(int n){
array = (T[])new Number[n];
}
public double getMedian(int size){
...
Arrays.sort(array);
...
}
}
Thanks!
arrayisnull. It cannot be. If you want to includenullwrite a customComparatorto handle it.ComparableTimSort.countRunAndMakeAscending? What's that going to tell the OP?