Hello!
I am currently working on an assignment and I have been stuck with a NullPointerException.
I am making a Priority Queue (PQ) as one part of the assignment i'm working on.
Firstly, if I add nodes into the queue in ascending order I do not get an error while adding to the queue (1,3,5,6, etc), but I do get the error when putting them out of order (4,2,5).
So this tells me there is a problem in my balance function.
Secondly, when removing Nodes from the PQ (input ascending order 4,5,6) it keeps removing the first one only (4,4,4). So there is some problem with my "leave" function.
This is the exact error:
"java.lang.NullPointerException
at HEAP.PQ.balance(PQ.java:68)
at HEAP.PQ.enter(PQ.java:54)
at HEAP.Dijkstras.<init>(Dijkstras.java:28)
at HEAP.Dijkstras.main(Dijkstras.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)"
Any help with finding where the error is occurring would be greatly appreciated!
Also any other suggestions are welcomed.
Thanks in advance!
Here is my PQ class:
public class PQ { public Node[] array = new Node[30]; public int max = 0; public PQ() { } //function to check if the PQ is empty, returns true if it is public boolean isEmpty() { if(array[1] == null) return true; else return false; } //function to add to the PQ public void enter(Node n) { int count = 1; if(array[1] == null) { array[1] = n; } else { while(array[count] != null) { count++; System.out.println(count); } array[count] = n; } max = max + 1; balance(max); } //function to balance the PQ after an add private void balance(int count) { Node temp; if(array[2] == null && array[3] == null) { return; } else { while(array[count/2].key > array[count].key) { if(count == 1) { if(array[1].key > array[count].key) { temp = array[1]; array[1] = array[count]; array[count] = temp; } break; } temp = array[count/2]; array[count/2] = array[count]; array[count] = temp; count = count/2; } } } //function to remove from the PQ public Node leave() { Node temp; //check if array only has 1 node if(array[2] == null && array[3] == null) { temp = array [1]; array[1] = null; max = max - 1; return temp; } temp = array [1]; array[1] = array[max]; array[max] = null; topBal(1); max = max - 1; return temp; } //function to balance PQ after remove private void topBal(int temp) { Node temp2; if(array[1] == null) return; else { //only if right is null, if so check if left is greater, if so swap if(array[temp*2] != null && array[(temp*2)+1] == null) { if(array[temp*2].key > array[temp].key) { temp2 = array[temp]; array[temp] = array[temp*2]; array[temp*2] = temp2; topBal(temp*2); } } //only if left is null, if so check if right is greater, if so swap if(array[temp*2] == null && array[(temp*2)+1] != null) { if(array[(temp*2)+1].key > array[temp].key) { temp2 = array[temp]; array[temp] = array[(temp*2) + 2]; array[(temp*2)+1] = temp2; topBal((temp*2)+1); } } //if both are not null, check which is larger & if they are larger then current node, swap if so if(array[temp] != null && array[temp*2] != null && array[(temp*2) + 1] !=null) { //if left is larger if(array[temp*2].key > array[(temp*2)+1].key && array[temp*2].key > array[temp].key) { temp2 = array[temp]; array[temp] = array[temp*2]; array[temp*2] = temp2; topBal(temp*2); } //if right is larger if(array[temp*2].key < array[(temp*2)+1].key && array[(temp*2)+1].key > array[temp].key) { temp2 = array[temp]; array[temp] = array[(temp*2) + 2]; array[(temp*2)+1] = temp2; topBal((temp*2)+1); } } } } }