Unable to read all the data existing in excel sheet using Java/Selenium script Unable to read all the data existing in excel sheet using Java/Selenium script selenium selenium

Unable to read all the data existing in excel sheet using Java/Selenium script


You have created data object using rowCount and colCount.

Object[][] data = new Object[rowCount][colCount];

But in the for loop condition is added like for (int i = 0; i <= rowCount; i++).So, this loop will try to execute one more time and hence ArrayIndexOutOfBoundsException is throwing.

Please change the condition as below

for (int i = 0; i < rowCount; i++) 

For Example, If the excel has only three rows and 2 columns, then data Object will be created as Object[][] data = new Object[3][2];. It can hold maximum of 3 rows. But the first for loop will be executed from index 0 to 3 (Totally 4 rows). When it tried to insert 4th row record, then ArrayIndexOutOfBoundsException is throwing.

Modified Code:

Object[][] data = new Object[rowCount][colCount];for (int i = 0; i < rowCount; i++) {    for (int j = 0; j < colCount; j++) {        data[i][j] = sh.getRow(i).getCell(j).getStringCellValue();        System.out.print(data[i][j] + "\t");    }    System.out.println();}

Edit:

getLastRowNum() method is 0 based.Refer the Doc. So, you need to modify your code as below in order to get all the rows data without an ArrayIndexOutOfBoundsException

Modified Code:

//Rowcount+1 needs to be added inorder to read the last rowObject[][] data = new Object[rowCount+1][colCount];for (int i = 0; i <= rowCount; i++) {    for (int j = 0; j < colCount; j++) {        data[i][j] = sh.getRow(i).getCell(j).getStringCellValue();        System.out.print(data[i][j] + "\t");    }    System.out.println();}