I'm assuming there's a logic in my error when trying to alter the queue, but I don't know what. I get a NullPointerException
for the line that reads if (eventqueue.peek() != null && eventqueue.peek().leavingtime < 61200)
. The queue is not empty, because I can print out elements from it when I try.
The program:
QueueLinkedList<Customer> eventqueue = new QueueLinkedList<Customer>();
QueueLinkedList<Customer> customerqueue = new QueueLinkedList<Customer>();
int numberserved = 0, totalidletime = 0, longestbreak = 0, idletime = 0, linelength = 0, longestline = 0;
String[] newarr = new String[8];
try {
File file = new File("customersfile.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
reader.readLine();
String line = null;
while ((line = reader.readLine()) != null) {
Customer newcust = new Customer();
if (line.trim().length() > 0) {
if (line.trim().charAt(0) == 'I') {
String next;
next = reader.readLine();
newcust.id = Integer.parseInt(line.substring(12));
newcust.arrtime = next.substring(14);
newcust.setarrtimesecs(newcust.arrtime);
newcust.setleavingtime(newcust.arrtime);
eventqueue.enqueue(newcust);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
for (int clock = 32400; clock < 86400; clock++) {
if (eventqueue.peek() != null && eventqueue.peek().leavingtime < 61200) {
if (eventqueue.peek().arrtimesecs < 32400 || eventqueue.peek().arrtimesecs == clock) {
if (eventqueue.peek().arrtimesecs == clock) {
if (customerqueue.peek() == null)
eventqueue.peek().waitingtime = 0;
else
eventqueue.peek().waitingtime = customerqueue.peek().leavingtime - eventqueue.peek().arrtimesecs;
}
if (eventqueue.peek().arrtimesecs < 32400)
eventqueue.peek().waitingtime = 32400 - eventqueue.peek().arrtimesecs;
customerqueue.enqueue(eventqueue.dequeue());
linelength++;
idletime = 0;
if (longestline != 0) {
if (linelength > longestline + 1)
longestline = linelength - 1;
}
}
if (customerqueue.peek().leavingtime == clock) {
numberserved++;
customerqueue.dequeue();
linelength--;
if (customerqueue.peek() == null)
idletime = 0;
}
while (customerqueue.peek() == null) {
idletime++;
totalidletime++;
if (idletime > longestbreak)
longestbreak = idletime;
}
}
else {
if (eventqueue.peek().arrtimesecs < 61200) {
if (eventqueue.peek().arrtimesecs == clock) {
if (customerqueue.peek() == null)
eventqueue.peek().waitingtime = 0;
else {
if (customerqueue.peek().leavingtime >= 61200)
eventqueue.peek().waitingtime = 61200 - eventqueue.peek().arrtimesecs;
}
customerqueue.enqueue(eventqueue.peek());
linelength++;
idletime = 0;
if (longestline != 0) {
if (linelength > longestline + 1)
longestline = linelength - 1;
}
}
if (customerqueue.peek().leavingtime == clock) {
numberserved++;
customerqueue.dequeue();
linelength--;
if (customerqueue.peek() == null)
idletime = 0;
}
while (customerqueue.peek() == null) {
idletime++;
totalidletime++;
if (idletime > longestbreak)
longestbreak = idletime;
}
}
else
eventqueue.peek().waitingtime = 0;
}
}
My Queue class:
Node first, last;
public class Node {
Customer ele;
Node next;
}
public QueueLinkedList() {}
public boolean isEmpty() {
return first == null;
}
public QueueLinkedList<Customer> enqueue(Customer ele) {
Node current = last;
last = new Node();
last.ele = ele;
last.next = null;
if (current == null)
first = last;
else
current.next = last;
return this;
}
public Customer dequeue() {
if (isEmpty())
throw new java.util.NoSuchElementException();
Customer ele = first.ele;
first = first.next;
return ele;
}
public Customer peek() {
Customer ele = first.ele;
return ele;
}
public String toString(Customer cust) {
Node curr = first;
return "" + curr.ele;
}