Getting NegativeArrayExcetion while reading excel file in selenium

265 views Asked by At

Getting error java.lang.NegativeArraySizeException: -1 as I read the data from Excel in selenium. I used TestNG framework. I used @DataProvider annotation for providing data from excel.

package testCases;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import utilities.ExcelReader;

public class dataProviderExcel {
    public static ExcelReader excel = null;

    @Test(dataProvider = "create")
    public void testData(String username, String password, String is_correct) {

        System.out.println(username + "---" + password + "---" + is_correct);

    }

    @DataProvider(name = "create")
    public static Object[][] dataSet() {

        if (excel == null) {

            excel = new ExcelReader("C:\\Users\\wagho\\OneDrive\\Documents\\saucedemo.xlsx");

        }

        String sheetname = "logindata";// sheetname from excel
        int rows = excel.getRowCount(sheetname);// return the count of rows present in sheet
        int colums = excel.getColumnCount(sheetname);// return the count of column present in sheet

        Object[][] dataset = new Object[rows - 1][colums];
        for (int rowNum = 2; rowNum <= rows; rowNum++) {

            for (int colNum = 0; colNum < colums; colNum++) {

                dataset[rowNum - 2][colNum] = excel.getCellData(sheetname, colNum, rowNum);

            }

        }

        return dataset;

    }

}

Following Image gives Detailed Information About Error:

1

There are 1 answers

1
Krishna Majgaonkar On

NegativeArraySizeException is thrown when one tries to create an array with a negative size. In your code the exception is thrown at

Object[][] dataset = new Object[rows - 1][columns];

Please check your excel reader utility.

int rows = excel.getRowCount(sheetname);

It's either returning 0 or 1

You try to debug the excel reader utility and observe what values you are getting for rows and columns