3

I have a program where I make an arraylist to hold some cab objects. I keep getting an error that what I get from the message is that java does not recognize that the arraylist has objects in it. This is the error that I am getting.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at edu.Tridenttech.MartiC.app.CabOrginazer.main(CabOrginazer.java:48)

This is the code that i am trying to get to work

public class CabOrginazer {

private static List<CabProperties> cabs = new ArrayList<CabProperties>();
private static  int count = 0;
private static boolean found = false;


public void cabOrginazer() 
{

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    CabRecordReaper reaper = new CabRecordReaper("C:/CabRecords/September.txt");
    CabProperties cabNum;

    for(int i = 0; i < 20; i++)
    {
        cabNum = new CabProperties();
        cabs.add(cabNum);
    }
    while(reaper.hasMoreRecords())
    {
            CabRecord file = reaper.getNextRecord();
            for(int j = 0; j < cabs.size(); j++)
            {
                if(cabs.get(j).getCabID() == file.getCabId())
                {
                    found = true;
                    cabs.get(j).setTypeAndValue(file.getType(), file.getValue(), file.getPerGallonCost());
                    cabs.get(j).setDate(file.getDateString());
                    break;
                }

            }

            if(found == false)
            {
                cabs.get(count).setCabId(file.getCabId());
                count++;
            }
            /*for(CabProperties taxi : cabs)
            {
                if(taxi.getCabID() == file.getCabId())
                {
                    found = true;
                    taxi.setTypeAndValue(file.getType(), file.getValue(), file.getPerGallonCost());
                    taxi.setDate(file.getDateString());
                    break;
                }


            }*/

    }


    for(CabProperties taxi : cabs)
    {
        System.out.print("cab ID: " + taxi.getCabID());
        System.out.print("\tGross earning: " +  taxi.getGrossEarn());
        System.out.print("\tTotal Gas Cost: " + taxi.getGasCost());
        System.out.print("\tTotal Service Cost: " +  taxi.getServiceCost());
        System.out.println();

    }


}

}

line 48 is the inside of that if statement where it says cabs.get(count).setCabId(file.getCabId()); with the little knowledge I have of Java. Java should know that there are elements inside of cabs and I should be able to set that id of the cab. What can cause Java not to recognize that the arraylist is populated?

2 Answers 2

7

The list isn't populated with an element at item count. Look at the exception: you've got 20 elements in the list, so the valid indexes are 0 to 19 inclusive. You're asking for record 20 (i.e. the 21st record). That doesn't exist.

It sounds like your block should be something like:

if (!found)
{
    CabProperties properties = new CabProperties();
    properties.setCabId(file.getCabId());
    // Probably set more stuff
    cabs.add(properties);
}

You may well be able to get rid of the count variable entirely - and your initial population of the list with dummy properties. It's very odd to populate a list like that to start with - that's typically something you do with an array which has a fixed size. One of the main benefits of using a List such as ArrayList is that it's dynamically sized.

Sign up to request clarification or add additional context in comments.

Comments

4

Java is recognizing the members just fine. You have 20 members in the array, indexed from index 0 through index 19.

You are asking for index 20, which does not exist.

The loop for:

while(reaper.hasMoreRecords())

must be running many more times than you expect, and your data is hitting the found == false if condition (which you can just say if (!found) { ... ) many times, and on the 21st time it fails with the index-out-of-bounds exception.

You should figure out how to use your debugger too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.