IndexOutOfBoundsException in Java LinkedList-Collection of common programming errors

This method is throwing an IndexOutOfBoundsException, and I don’t understand why as I’ve guarded against it.

private static boolean firstLoop(int custNo, LinkedList stock, LinkedList custs, Random generator, String colour, int col, Tracking tracking) {

    if ((stock.get(tracking.getLast(col)) != null) && (stock.get(tracking.getLast(col)).getLength() >= custs.get(custNo).getLength())) {

        stock.get(tracking.getLast(col)).length = Cut.cut(stock.get(tracking.getLast(col)).getLength(), custs.get(custNo).getLength(), colour);
        if (stock.get(tracking.getLast(col)).length < 5) {

            stock.remove(tracking.getLast(col)); //**CAUSES EXCEPTION**

            tracking.add();// recycle

        }
        return true;
    } else {
        for (int j = tracking.getLast(col) + 1; j < stock.size(); j++) {

            if ((stock.get(j).getLength() >= custs.get(custNo).getLength())) {
                // pipe is long enough, cut away the desired length
                stock.get(j).setLength(Cut.cut(stock.get(j).getLength(), custs.get(custNo).getLength(), colour));

                tracking.setLast(col, j);

                return true;
            }
        }

        // no suitable pipes available, order new one of correct colour with
        // random length 100-200 then cut from it, add to arraylist
        Pipe temp2 = new Pipe(col, generator.nextInt(101) + 100);
        temp2.setLength(Cut.cut(temp2.length, custs.get(custNo).length, colour));
        stock.add(temp2);
        tracking.setLast(col, stock.size() - 1);
        return false;
    }

}

I’ve found that the marked line is the one that causes the exception (the program runs perfectly when it’s commented out). However, I’m confused because tracking.getLast(col) works perfectly in the lines above it, and the remove function is not inside an iterator or a loop. Here is the Tracking class:

public class Tracking {

static int lastR=0;
static int lastG=0;
static int lastY=0;


public void setLast(int col, int last){
    if(col==0){
        lastR=last;
    }else if(col==1){
        lastG=last;
    }else if(col==2){
        lastY=last;
    }else{
        System.out.println("Colour does not exist");
    }
}

public static int getLast(int col){
    if(col==0){
        System.out.println(lastR+" red");
        return lastR;
    }else if(col==1){
        System.out.println(lastG+" green");
        return lastG;
    }else{
        System.out.println(lastY+" yellow");
        return lastY;
    }
}

And this is the use of the method that throws the error:

if ((yellowStock.get(Tracking.getLast(2)) != null) && (yellowStock.get(Tracking.getLast(2)).getLength() >= custs.get(i).getLength())) {

  firstLoop(i, yellowStock, custs, generator, "yellow", 2, recycle);
} 

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.LinkedList.checkElementIndex(Unknown Source) at java.util.LinkedList.get(Unknown Source) at NextFit.next(NextFit.java:26) at Main.main(Main.java:58)

Thank you very much guys.

  1. The problem is as follows:

    LinkedList.remove() comes in two flavours:

    1. LinkedList.remove(int index), which removes the element at the specified index
    2. LinkedList.remove(Object o), which removes the specifed element

    Further, tracking.getLast(int col) returns int, so when you call

    stock.remove(tracking.getLast(col));
    

    the version of remove() that is called is number 1, not number 2 as you wanted.

    Try calling stock.remove(col); or whatever makes sense.

    As an aside, your coding style could use some improvement, especially the use of static fields and methods in Tracking instead of instance fields and methods.