1

This is the entire bulk of the code.

for (int i = 0; i <= 1; i++, column++, row++) {
    // read cell, input coordinates: (column, row)
    Cell c1 = sh1.getCell(column, row);
    Cell c2 = sh1.getCell(column + 1, row);
    Cell c3 = sh1.getCell(column + 2, row);
    Cell c4 = sh1.getCell(column + 3, row);

    // return string content
    String locationCode = c1.getContents();
    String locationName = c2.getContents();
    String description = c3.getContents();
    String address = c4.getContents();

    driver.findElement(By.id("tab7")).click();
    driver.findElement(By.id("subtab2")).click();
    driver.findElement(By.id("new")).click();
    driver.findElement(By.id("ExtLocationCode")).clear();
    driver.findElement(By.id("ExtLocationCode")).sendKeys(locationCode);
    driver.findElement(By.id("LocationName")).clear();
    driver.findElement(By.id("LocationName")).sendKeys(locationName);
    driver.findElement(By.id("Description")).clear();
    driver.findElement(By.id("Description")).sendKeys(description);
    driver.findElement(By.id("address1")).clear();
    driver.findElement(By.id("address1")).sendKeys(address);
    driver.findElement(By.id("save")).click();
    driver.findElement(By.id("actions")).click();
    driver.findElement(By.id("save")).click();
}

After running it, it says: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

and it doesn't proceed to the next columns and rows of the cells in the excel file. What is wrong with my code?

5
  • 1
    Where does the error occur? And are you reading a diagonal? Because you are increasing both row and column in each loop. Commented Jul 7, 2015 at 7:49
  • 2
    What is the structure of whatever it is you are trying to read? You seem to be trying to read more than what you have. Commented Jul 7, 2015 at 7:49
  • How many rows and columns are there in the sheet? Commented Jul 7, 2015 at 7:50
  • you should go till your data length using loop.. Commented Jul 7, 2015 at 7:50
  • Why you are incrementing column++ in the loop? It assume it should be column = 0 and then column+1 and so on for next columns. Commented Jul 7, 2015 at 7:50

1 Answer 1

4

You are incrementing your column in your for loop definition , afterwards you do:

Cell c1 = sh1.getCell(column,row);
Cell c2 = sh1.getCell(column+1,row);
Cell c3 = sh1.getCell(column+2,row);
Cell c4 = sh1.getCell(column+3,row);

I don't think you need to increment your column in the for loop definition for(int i = 0; i<=1; i++, column++, row++) Unless you have a really strange excell sheet layout. Try for(int i = 0; i<=1; i++, row++)

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

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.