Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:47:37+00:00 2026-06-03T06:47:37+00:00

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

  • 0

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<Pipe> stock, LinkedList<Customer> 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:47:38+00:00Added an answer on June 3, 2026 at 6:47 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having this method: public Boolean isCorrect() { return correct; } I can't write: @XmlType(propOrder
This method keeps throwing the exception in the title, and I cannot find a
Should I be using this method of throwing errors: if (isset($this->dbfields[$var])) { return $this->dbfields[$var];
Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {
I am using the ValidatesOnExceptions=true method of WPF validation. This involves throwing an exception
This method works as expected - it creates a JTree with a root node
This method that draws my tiles seems to be quite slow, Im not sure
This method is working totally right in matlab. but, when I compiled it in
With this method declaration (no overloads): void Method(double d) { // do something with
Take this method /** * @return List of group IDs the person belongs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.