Phil Mendelsohn wrote:

> public class PriorityQueue{
>
>     public qItem [] Q;

You define Q here but give no value which means Q is null.

>
>     public int qSize;
>     public int [] where;
>
>     public class qItem{
>         public int item;
>         public int key;
>
>         public qItem(int item, int key){
>             //consructor
>             this.item = item;
>             this.key = key;
>         }
>     }
>
>     public PriorityQueue(int maxItem){
>         //constructor of an empty queue: see below
>         qItem [] Q = new qItem[maxItem];

You define Q again here in the constructor which means Q is a local
variable to the constructor and you are NOT referencing the instance
variable Q defined in the class.

>
>         int [] where = new int[maxItem];
>         for( int j=0; j < maxItem; j++){
>             where[j] = -1;
>             Q[j] = new qItem(-2, -3);
>             System.out.println(where[j] + " " + Q[j].item +
>                                " " +  Q[j].key);
>         }
>         int qSize = 0;
>     }//end constructor
>
>     void insert(int item, int key){
>         //insert the item with given key as its priority
>         qSize++;
>         System.out.println("qSize:  " + qSize);
>         System.out.println("Item: " + item);
>         System.out.println("Key: " + key);
>         for(int i = 0; i < qSize; i++){
>             System.out.println(Q[i].item);
>             System.out.println(Q[i].key);
>         }
>     }

Any reference to Q here will result in a NullPointerException because
again, you are referencing the instance variable Q defined in the class
but never initialized.  Remove the qItem[] from the Q in the public
constructor so that you are referencing the instance variable Q.

Doing so and running with a queue size of 1 and an item/key value of 1/1,
I see the following:

-1 -2 -3
qSize:  1
Item: 1
Key: 1
-2
-3

Is this more of what you are looking for?


--
Perry Hoekstra
E-Commerce Architect
Talent Software Services
perry.hoekstra at talentemail.com