My program won't run.. I keep getting a nullPointerException but I have no idea why.. Thanks! The program is supposed to create a simulation for a line

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

public class Prog5 {

/**
 * @param args
 */
public static void main(String[] args) {
    Simulation simulation = new Simulation(10);
    simulation.run();
}

}

-----------------------------------------------------------------------------
import java.util.Random;

public class Simulation {
private MyQueue<Customer> arrivalQ; // = new MyQueue<Customer>;
private MyQueue<Customer> waitQ; // = new MyQueue<Customer>;
int busy = 0;
int time = 0;

public Simulation(int n) {
    Random rand = new Random();
    for (int id = 1; id <= n; id++) {
        time = time + rand.nextInt(3);
        Customer customer = new Customer(id, time);
        arrivalQ.enqueue(customer);
    }
}

public void run() {
    Customer c = null;
    while (!arrivalQ.isEmpty()) {
        if (busy < 1) {
            busy--;
        } else if (busy == 1) {
            System.out.println(c.getArrivalTime());
            busy--;
        }
        while (arrivalQ.peek().getArrivalTime() == time) {
            c = arrivalQ.dequeue();
            waitQ.enqueue(c);
        }
        if (busy == 0 && !waitQ.isEmpty()){
            c = waitQ.dequeue();
            busy = 2;
        }
        time++;
    }
}

}

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

public class Customer {

private int id;
private int arrivalTime;


public Customer(int id, int arrivalTime) {
    this.id = id;
    this.arrivalTime = arrivalTime;
}

public int getId() {
    return id;
}

public int getArrivalTime() {
    return arrivalTime;
}

}

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

/**
*
* @author Ryan Lord
*
*/
public class MyQueue<E> {
Node<E> front = null;
Node<E> rear = null;

/**
 * Adds an item to the queue
 * @param item - E
 */
public void enqueue (E item){
    if (rear == null){
        rear = new Node<E> (item, null);
        front = rear;
    } else {
        rear.next = new Node<E> (item,null);
        rear = rear.next;
    }
}

/**
 * Removes the front item from the queue
 * @return E - the front item in the queue
 */
public E dequeue() {
    if (front == null){
        return null;
    } else {
        E temp = front.data;
        front = front.next;
        return temp;
    }
}

public E peek() {
    if (front == null) {
        return null;
    } else {
        return front.data;
    }
}

/**
 * Tells whether the queue is empty or not
 * @return true or false - depending on if the
 * queue is empty or not
 */
public boolean isEmpty(){
    return (front == null);
}



/***************************************************************
 * Node inner class
 */
private static class Node<E>
{
    private E data;
    private Node<E> next;

    public Node(E data, Node<E> next)
    {
        this.data = data;
        this.next = next;
    }
}

}

What variable has the null value? why doesn't the variable have a valid value?

Post the full text of the error message. It shows the line number where the error occurred.

Sorry i meant to put that...

Exception in thread "main" java.lang.NullPointerException
at Simulation.<init>(Simulation.java:16)
at Prog5.main(Prog5.java:8)

Look at line 16 in the program. What variable on that line has a null value? Then backtrack in the code to find why that variable does not have a valid value. If you can't see what variable has a null value, add a println just before line 16 that prints the values of all the variables on line 16.

Edited 4 Years Ago by NormR1

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