0

I'm writing a code to read some records from a file and sort them in a special manner. I tried a code like this:

public class Main {

    static class judgement implements Comparable<judgement> {
        public int q;
        public int d;
        public int r;

        public int compareTo(judgement j) {
            int k = ((judgement) j).q;
            return 0;
        }
    }

    public static void method() throws Exception {
        judgement[] judgements;
        judgements = new judgement[18425];
        try {
            // fill the "judgements" array
        } finally {
            Arrays.sort(judgements);
        }
    }

    public static void main(String[] args) throws Exception {
        method();
    }

}

But I get the error NullPointerException in the function compareTo. Can anybody help me with this problem?

4 Answers 4

5

That is because the array is filled with null pointers. The comparator can't compare the null objects with each other.

Creating an array doesn't initialise each element of the array.

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

2 Comments

but I have initialized the array, I've just omitted some parts of the code in try { // fill the "judgements" array } to make it short here
Make sure that really every single element is initialised.
2

You are initializing arrays with null values.

judgements = new judgement[18425];

And you are not checking the null Values in compare to. you have to use if statement.

public int compareTo(judgement j) {
          int k =0;
    if(j!=null){
    k = ((judgement) j).q;
    } 

                return 0;
            }

1 Comment

It solved the problem, because I'd forgot to fill in 1 element of the array.
2

Make sure to have only non-null elements in your array regarding your implementation of compareTo(). Otherwise, in your function compareTo(), calling "q" attribute on a supposed existing judgement causes a NullPointerException.

Furthermore, avoid tabs, prefer using Collections. In your case, you can use TreeSet objects instead of arrays since they internally use compareTo() method of elements into them.

2 Comments

You are right, I had declared 18425 elements in the array and initialized only 18424 elements.
Yes :) Benefit of using Collections is that you needn't to hard-coded size of array. I'm saying array because Collection subclasses are all based on array. The only benefit that I found for using Arrays instead of Collections was because before JDK 5, we couldn't have a strong reference type. Since JDK 5, we can use generics for that : Set<Judgement> = new TreeSet<Judgement>();
2

What's probably happening is that some elements of your array are null.

It's good practice to include a null check in your compareTo function. Also, right now your compareTo function is non-functional: it always returns 0 -- this is like saying that all items are equal.

Your other problem is that it looks like you're using a try{}finally{} for things that it shouldn't be used for.

Comments

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.