0

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);
        }
    }
}
Nir Alfasi
  • 51,812
  • 11
  • 81
  • 120
  • @Braj: I did try the Generic one as well, It did not work and was giving the same exception. I have updated the complete code. Check, please? –  Sep 13 '14 at 04:25
  • change `while (front + 1 != 3) {` into `if (front + 1 != 3) {` You have make it more complicated. Just use `List` instead of static `array` – Braj Sep 13 '14 at 04:45
  • @Braj : Tried, the problem is in compareTo method only. It works fine by the time of insertion of elements –  Sep 13 '14 at 04:51
  • Solved! Thanks everyone for helping :) –  Sep 13 '14 at 04:57
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Andreas detests censorship Sep 14 '20 at 17:41

2 Answers2

2

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)

Erwin Bolwidt
  • 29,014
  • 15
  • 50
  • 74
  • He is, actually. There's a really weird, highly inadvisable call in the constructor that tries to insert the items into the array. That call is bugged, though. – user2357112 Sep 13 '14 at 04:32
  • It's bugged in more ways than that, though, because the `while` loop makes no sense, especially its termination condition. Maybe it was an attempt to shift the array elements over to make room? – user2357112 Sep 13 '14 at 04:38
  • Yeah hmm. Probably better to just use a `List list = new ArrayList<>();` – Erwin Bolwidt Sep 13 '14 at 04:40
  • @user2357112: Oh, that while loop was just a check, you can ignore that. –  Sep 13 '14 at 04:43
  • @ErwinBolwidt : I did make the variable front static but that did not work either –  Sep 13 '14 at 04:43
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
        }
    }
}
Braj
  • 45,615
  • 5
  • 55
  • 73
  • Ahhmm, pr is reference variable of Object. How else was I supposed to code it? I am clueless if it has to be some other way –  Sep 13 '14 at 04:30
  • 1
    [`compareTo` is *supposed* to throw a `NullPointerException` when passed a `null`.](http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T-) – user2357112 Sep 13 '14 at 04:31