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;
}
}
}