So right now I am working on small simulator, that simulates a store. This store has multiple check out lanes, or in this case queues. Now what I am trying to do is make it so I can move over a Customer from one queue, or line, to the another one if there is another open line, and they are second in line. So essentially I am taking the front.next
Node, and moving it over to become the front Node in the whatever queue I decide to move it to. This is the exact error code I am getting:
Exception in thread "Timer-0" java.lang.NullPointerException
at unit8_Task1.Store.moveLanes(Store.java:220)
at unit8_Task1.Store.access$9(Store.java:184)
at unit8_Task1.Store$6.run(Store.java:96)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
and here is the code that is involved with moving the customer:
private void moveLanes(LineQueue<Customer> l){ //modification
LineQueue<Customer> largestLine = null;
LineQueue<Customer> lineToAdd = null;
if(superExpress.size() == 0 && superExpress.getContFreeTime() >= 30){
lineToAdd = superExpress;
for(int i = 0; i < queues.length; i++){
if(largestLine == null){
queues[i] = largestLine;
}else if(queues[i].size() > largestLine.size()){
largestLine = queues[i];
}
}
}else if(expressOne.size() == 0 && expressOne.getContFreeTime() >= 30){
lineToAdd = expressOne;
for(int i = 0; i < queues.length; i++){
if(largestLine == null){
queues[i] = largestLine;
}else if(queues[i].size() > largestLine.size()){
largestLine = queues[i];
}
}
}else if(expressTwo.size() == 0 && expressTwo.getContFreeTime() >= 30){
lineToAdd = expressTwo;
for(int i = 0; i < queues.length; i++){
if(largestLine == null){
queues[i] = largestLine;
}else if(queues[i].size() > largestLine.size()){
largestLine = queues[i];
}
}
}
Customer c = largestLine.getNextInLine();
largestLine.remove(c);
lineToAdd.offer(c); //adds the customer to the smallest line
lineToAdd.setIPH(items); //this will be added after the customer is generated in the above method
if(lineToAdd.getMaxLength() < lineToAdd.size()){ //if the maximum length is now smaller than the current size
lineToAdd.setMaxLength(lineToAdd.size());
}
add(c);
}
The add method at the end is what adds objects, or in this case Customers, to the front of a selected queue. That has been working when generating new customers, so there should not be a problem there. Lastly, there is the code I have for the LineQueue
itself:
private Node<Customer> front;
private Node<Customer> ref;
public Customer getNextInLine(){return front.next.data;};
@Override
public boolean offer(Customer e){
if(size == 0){
front = new Node<Customer>(e);
}else if(size == 1){
front.next = new Node<Customer>(e);
ref = front.next;
}else{
ref.next = new Node<Customer>(e);
ref = ref.next;
}
size++;
CPH++;
return true;
}
I am guessing this is occurring because the first.next.data
is not actually storing the Customer
object correctly. I also tried type casting to Customer (Customer)first.next;
instead, but I got the same error.