i am trying to load up my ObjectQueue variable objQueue with a task to run some operations on, and there i have a NullPointerException error. the error happens at: objQueue[0].insert(Task); I have declared the ObjectQueue variable in a similar fashion on other programs and have had no problem. sort of lost on the null pointer error.

the code is:

import java.util.Scanner;
import java.io.*;
public class Driver 
{
    public static void main(String [] arg) throws IOException  
    {
        Scanner Scan = new Scanner(new File("mfq.txt"));
        LineWriter lw = new LineWriter("csis.txt");

        //numbers of queue in the system
        int queues = 4;

        //Array holding priority Queues of objects
        ObjectQueue [] objQueue = new ObjectQueue [queues];


        //Variable to hold any process/task to be pass to the CPU
        //from the text file or a queue
        Process Task;

        //variable to finish all process when set to false
        boolean END;

        //CPU object
        CPU Core = new CPU();

        //variable to hold time elapsed in the machine
        int Machine_Time = 0;

        // q set to -1 as initial value
        //int q;

        //move all process into objQueue[0]
        while (Scan.hasNext())
        {
            Task = new Process (Scan.nextLine(), lw, 0);
            objQueue[0].insert(Task);


        }

        //set all process into the first priority queue
        do
        {
            //set END to false for a continuous loop until all queues are empty and q to -1 
            END = false;
            int q = -1;

            //check if there is any process in the input Queue
            if (!objQueue[0].isEmpty())

            {
                //check if process needs to be send to the CPU
                if (((Process)objQueue[0].query()).getSendTime() == Machine_Time)

                {
                    q = 0;
                }
            }

            //if process does not have to be send to CPU, look in priority queues
            if (q != 0)
            {
                for (int i = 1; i < queues; i++)
                {
                    if (!objQueue[i].isEmpty())

                    {
                        q = i;
                        break;
                    }
                }
            }

            if (q > -1)
            {
                Task = (Process) objQueue[q].remove();


                if (Core.Busy())
                {
                    if (q < queues - 1)
                        q++;

                    Task.setQueue(q);
                    objQueue[q].insert(Task);

                }
                else
                {
                    if (q == 0)
                        Core.getProcess(Task, Machine_Time);
                    else
                        Core.getProcess(Task);
                }

                Core.Process();

                //if  CPU is not busy
                if (!Core.Busy())
                {
                    Task = Core.freeCPU(Machine_Time);

                    if (Task.getTimer() == 0)
                        Task.EndProcess();
                    else
                    {
                        if (q < queues - 1)
                            q++;

                        Task.setQueue(q);
                        objQueue[q].insert(Task);
                    }
                }

                //check if all queues are empty
                for (int c = 0; c < queues; c++)
                {
                    if (!objQueue[c].isEmpty())
                    {
                        END = true;
                        break;
                    }
                }
            }
        }while (END);
    }
}

-------------------

public class ObjectQueue
  {
    private Object[] item;
    private int front;
    private int rear;
    private int size;

    public ObjectQueue(){
        size = 100;
        item = new Object[size];
        front = size-1;
        rear = size-1;
    }

    public ObjectQueue(int max){
        size = max;
        item = new Object[size];
        front = size-1;
        rear = size-1;
    }

    public boolean isEmpty(){
        return front == rear;
    }

    public boolean isFull(){
        return rear == size-1 ? front == 0 : front == rear+1;
    }

    public void clear(){
        front = size-1;
        rear = size-1;
    }

    public void insert(Object x){
        if (isFull()){
            System.out.println("Insert Runtime Error: Queue Overflow");
            System.exit(1);
        }
        if (rear == size-1)
            rear = 0;
        else
            rear++;
        item[rear] = x;
    }

    public Object remove(){
        if (isEmpty()){
            System.out.println("Remove Runtime Error: Queue Underflow");
            System.exit(1);
        }
        if (front == size-1)
            front = 0;
        else
            front++;
        return item[front];
    }

    public Object query(){
        if (isEmpty()){
            System.out.println("Query Runtime Error: Queue Underflow");
            System.exit(1);
        }
        if (front == size-1)
            return item[0];
        else
            return item[front+1];
    }


}

Line 14, you initilise the array of ObjectQueue, but you have not instantiated the class object in the array. So each index in the array does not have an ObjectQueue object in it but null... You need to go through your array and instantiate each of the index.

for (int i=0; i<objQueue.length; i++) { objQueue[i] = new ObjectQueue(); }

Edited 4 Years Ago by Taywin

thanks for the reply. ive added what you suggested and it seems to be doing what it is supposed to. now to work on my output! my project isnt outputting to the terminal window as is, so still some work left to be done. will let you know when i get things working (and will close discussion)

This article has been dead for over six months. Start a new discussion instead.