crchin
crchin

Reputation: 9673

java.lang.IndexOutOfBoundsException: Index: xx, Size: xx

I am loading a .csv file with comma delimiter by using jdom parser to split the data and insert into database.

but somehow I have hit this error as title mentioned. the .csv file contains of over 200k of records.

when it reads until around 4000 records , the program was thrown this error. index and size are the same value but before that all the records can be inserted successfully. Just wondering why it goes until almost 4k only hit this error.

this is my for loop :

for(int x = 0; x < FLODS.getBufferSize(); x++)

But I did a search on internet : they are sugguesting this method..

for(int x = 0; x < FLODS.getBufferSize()-1; x++)

Any idea? Thanks :)

Upvotes: 1

Views: 37962

Answers (4)

Dana
Dana

Reputation: 35

List<WebElement> div1=driver.findElements(By.xpath(".//*[@class='art_title']"));        

for(int i=0;i<=div1.size();i++)
{           
    System.out.println(div1.get(i).getText());          
    Thread.sleep(1000);
}

Instead of the above format I changed it into this format:

List<WebElement> div1=driver.findElements(By.xpath(".//*[@class='art_title']"));
String[] abc = new String[div1.size()];

int i= 0;

for (WebElement e : div1) 
{
        abc[i] = e.getText();
        i++;            
        System.out.println(e.getText());
}

Upvotes: 0

Varun Gattu
Varun Gattu

Reputation: 43

This is a mistake when you have a nested For loop and try to access the 1st variable in the second one.

for(int i = 0;i<something.size(); i++) {
     //Some manipulations here..
     for(int j = 0;j<somethingElse.size(); j++) {
          //using blah(i) instead of blah(j) causes this issue
     }
}

Upvotes: 0

root
root

Reputation: 1

java.lang.IndexOutOfBoundsException is a type of exception that appears when index of some sort (such as to an array, to a string, or to a vector) is out of range so check for the memory allotted. that's all one can say without viewing the code before and within the for loop

Upvotes: 0

Rahul
Rahul

Reputation: 45050

java.lang.IndexOutOfBoundsException: Index: xx, Size: xx

A very common Exception. This clearly states that you're trying to access an Index of xx, where the Size of the ArrayList(for example) is also xx. Always remember that if the size is xx, you can access the index only till xx-1. If you try to access xx index, where the size is also xx, you are bound to get this error.

Hence the suggestion from Internet, asking you to traverse only till -1 of whatever size.

Though your FOR loop seems to be okay, but may be your are trying to access some index inside the loop which is 1 more than the current x value at some point.

Upvotes: 7

Related Questions