0

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!

6
  • Can you provide an MCVE that demonstrates this behavior? Commented Sep 18, 2014 at 16:57
  • 4
    An element of array is null. It cannot be. If you want to include null write a custom Comparator to handle it. Commented Sep 18, 2014 at 16:57
  • Check if items in your arrays are null.. Commented Sep 18, 2014 at 16:58
  • can you share more of your code? its impossible to follow Commented Sep 18, 2014 at 17:02
  • @AmirAfghani of ComparableTimSort.countRunAndMakeAscending? What's that going to tell the OP? Commented Sep 19, 2014 at 7:18

3 Answers 3

1

Well obiviously there is not enough code here to understand but as for the ComparableTimSort (where the exception is actually comes from) class where you pass your T to, Not all the T types you pass (Number class) implement the Comparable interface in order to check if that is your problem try and pass you T with a Comparable cast

Sign up to request clarification or add additional context in comments.

Comments

1

You have to write a custom Comparator in order to sort using Arrays.sort().

Arrays.sort(array, yourComparator);

Comments

1

You can do

  1. What vhax suggested. More specifically

    Arrays.sort(array, new Comparator<T>{
        @Override
        public int compare(T arg0, T arg1)
        {
            ...
        }
    });
    
  2. What crazyPixel suggeseted, more spefically

    public class CircularBuffer<T extends Number & Comparable<T>>
    

This way, you are restricting on T to be more specific. This is called "multiple bounds" http://docs.oracle.com/javase/tutorial/java/generics/bounded.html

1 Comment

When I first reviewed this I didn't understand why you were copying two different answers into a new answer (generally frowned on). Then I checked and you were actually providing significant additional information so you get a +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.