We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,278 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Descrete Event Simulator error

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

}

2
Contributors
3
Replies
10 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
rlord3534
Newbie Poster
2 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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)

rlord3534
Newbie Poster
2 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0655 seconds using 2.71MB