0

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.

FyreeW
  • 347
  • 1
  • 2
  • 15
  • Please post the code for Store class. More specifically, line 220 in that class. – Pavan Dittakavi Nov 01 '15 at 14:34
  • @OliverCharlesworth Line 220 is in the moveLanes method. This is line 220: Customer c = largestLine.getNextInLine(); . – FyreeW Nov 01 '15 at 14:37
  • _largestLine_ has no value (is null)! In all your loops the else part is only true when _largestLine_ has a value. But you only assign a value to _largestLine_ in these else cases. Therefore it will always be null! – Iltis Nov 01 '15 at 14:37
  • @Iltis are you talking about the for loops? Because what it does is it sets the initial value when it is first starting the loop, and then after that it no longer has a null value. The only reason the values are null is because I have yet to set which queues are going to be used. They are set in the for loops. However I do see how if none of the three queues are open it could cause a problem. – FyreeW Nov 01 '15 at 14:49
  • _lineToAdd_ is initialized before every loop, but i don't find any assignment to _largestLine_, with exception of your **else if** cases. So the value of _largestLine_ is null when it goes in any of your for loops. In every loop the assignment can only happen if the **else if** case is true. Which is never the case, because _if(largestLine == null)_ is always true, as long as _largestLine_ is null. So _largestLine_ can only become not null if it is not null, which can not happen. – Iltis Nov 01 '15 at 15:09

0 Answers0