Home > Blockchain >  I am new with working with priority queues and have formatted this code wrong.What is the error in m
I am new with working with priority queues and have formatted this code wrong.What is the error in m

Time:09-09

Error in the output box is : Exception in thread "main" java.lang.NullPointerException: Cannot assign field "value" because "this.priorityqueue[this.count]" is null at PriorityQueue.enQueue(PriorityQueue.java:16) at Main.main(Main.java:4)

It is having operations like enqueue,dequeue,peek of Priority Queue. Mostly showing the error in Enqueue part.

public class PQ {
    public int value;
    public int priority;
}

public class PriorityQueue {
    public PQ[] priorityqueue;
    public int count;
    
    public PriorityQueue(int size){
        this.count = 0;
        this.priorityqueue = new PQ[size];
        System.out.println("The Priority Queue is create with the size of :"   size);
    }

    public void enQueue(int element,int priority){
        if (count == priorityqueue.length){
            System.out.println("Priority Queue Overflow!");
        }
        else {
            priorityqueue[count].value = element;
            priorityqueue[count].priority = priority;
            count  ;
        }
    }
    
    public int peekprio(){
        int max = Integer.MIN_VALUE;
        int index = -1;
        for (int i = 0;i < count;i  ){
            if (priorityqueue[i].priority > max){
                max = priorityqueue[i].priority;
                index = i;
            } else if (priorityqueue[i].priority == max && index > -1 && priorityqueue[index].value < priorityqueue[i].value){
                index = i;
            }
        }
        return index;
    }
    
    public int peek(){
        if (count == 0){
            System.out.println("Priority Queue Underflow!");
            return -1;
        }
        else {
            int index = -1;
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < count; i  ) {
                if (priorityqueue[i].priority > max) {
                    max = priorityqueue[i].priority;
                    index = i;
                }
                else if (priorityqueue[i].priority == max && index > -1 && priorityqueue[index].value < priorityqueue[i].value){
                    index = i;
                }
            }
            return priorityqueue[index].value;
        }
    }

    public void deQueue(){
        if (count == 0){
            System.out.println("Priority Queue Underflow!");
        }
        else {
            int element = priorityqueue[peekprio()].value;
            int index = peekprio();
            for (int i = index;i < count;i  ){
                priorityqueue[i] = priorityqueue[i   1];
            }
            count--;
            System.out.println("Value deQueued :"   element);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        PriorityQueue pq = new PriorityQueue(5);
        pq.enQueue(1,0);
        pq.enQueue(3,3);
        pq.enQueue(5,5);
        pq.enQueue(2,2);
    }
}

CodePudding user response:

You are modeling your priority queue internally with an array of PQ objects, but you are missing the creation of the PQ object itself, so when you try to assign a value and priority to the queue it tries to set it to an object that does not exist yet.

When you do this: this.priorityqueue = new PQ[size]; on the constructor you are only creating the array, you still need to create the individual objects when you whant to add them to the array.

To do that change this part on the enQueue method to add the new PQ():

else {
   priorityqueue[count] = new PQ();
   priorityqueue[count].value = element;
   priorityqueue[count].priority = priority;
   count  ;
}

Ideally you'd want to create a constructor for that on the PQ class instead of setting the properties directly from outside.

Hope that helps.

  • Related