简体   繁体   中英

Null pointer Exception in CompareTo method

Structure of my class:

public class Priorityy implement Comparable {
    public int compareTo(Object pe) {
        Priorityy p = (Priorityy) pe;
        if (this.key < p.key) {    
            return 1;
        } else if (this.key > p.key) {
            return -1;
        } else {
            return 0;
        }
    }
}

Th problem is that p.key is always null, why exactly is that? I have my array initialized with elements in it but it always throws NullPointerException whenever I try Arrays.sort(arr) .

How can I fix this?

Edit: Here is the complete code and print did print the elements of array arr :

import java.util.Arrays;

class Priorityy implements Comparable {
    int size;
    int front = 0;
    int rear = 0;
    static Priorityy[] arr = new Priorityy[3];
    int key;
    String value;

    public Priorityy(int key, String value) {
        this.key = key;
        this.value = value;
        insert();
    }

    public void insert() {
        arr[front] = this;
        System.out.println(arr[front].value);
        while (front + 1 != 3) {
            front = front + 1;
        }
    }

    public Priorityy remove() {
        Priorityy x = arr[front];
        front = front - 1;
        return x;
    }

    public int compareTo(Object pe) {
        Priorityy p = (Priorityy) pe;
        if (this.key < p.key) {
            System.out.println(p.key);

            return 1;
        } else if (this.key > p.key) {

            System.out.println("3");
            return -1;
        } else {
            System.out.println("4");
            return 0;
        }
    }

    public static void main(String... s) {
        new Priorityy(10, "Watch");
        new Priorityy(40, "Laptop");
        new Priorityy(60, "Wallet");
        Arrays.sort(arr);
        for (Priorityy element : arr) {
            System.out.println(element.key);
            System.out.println(element.value);
        }
    }
}

You're putting things into your array in a really strange manner. But given that, the problem is that you're not using a static field to store the next position to insert an element into, so the next time you create an instance of Priorityy , the field first contains the value zero again. So you're inserting all three objects into element zero of the array.

Change one line of your code and it will work:

int front = 0;

To:

static int front = 0;

I don't see where you are using size and rear but you probably want these to be static too.

One other suggestion: Java has a nice short syntax for increasing or decreasing the value of a variable by one using the ++ or -- operator, so you can shorten things by saying:

front++;

instead of

front = front + 1;

(and front-- instead of front = front - 1 )

As per your code

Priorityy p = (Priorityy)pe;
                         ^^ ---------- this is null

You have null object in the array. Handle null object gracefully.

For example

if(pe instanceof Priorityy){ // return false for null object
     // your code goes here
}

Better use Generic Comparable and use Integer.compare(int,int) to compare two int values.

class Priorityy implements Comparable<Priorityy> {
    public int compareTo(Priorityy pe) {
        if (pe != null) {
            return Integer.compare(this.key, pe.key);
        } else {
            // return what ever if pe is null
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM