TransWikia.com

Selenium Dataprovider returns a NegativeArrayException when called from different class

Software Quality Assurance & Testing Asked by user12730187 on October 25, 2021

I have an excel which contains multiple worksheets in it. Each worksheet contains multiple columns. I have a class like this.

public class dptwo extends dpone {

@Test(dataProviderClass = testdp.class, dataProvider = "dp")
 public static void methodone(String a, String b , String c)
 {
     System.out.println(a);
     System.out.println(b);
     System.out.println(c);
  }

Now i have another class, in the same package named "testdp.java"

public class testdp{ 
@DataProvider(name="dp")
public Object[][] getData(Method m) {

String sheetName = m.getName();
int rows = excel.getRowCount(sheetName);
int cols = excel.getColumnCount(sheetName);
Object[][] data = new Object[rows - 1][cols];
    for (int rowNum = 2; rowNum <= rows; rowNum++) { 
             for (int colNum = 0; colNum < cols; colNum++) {
        data[rowNum - 2][colNum] = excel.getCellData(sheetName, colNum, rowNum); 
    }
}
return data;

}

When I run, I’m getting an error as mentioned below.

 [ERROR] [Error] java.lang.NegativeArraySizeException
at temp.dpthree.getData(dpthree.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131)
at org.testng.internal.Parameters.handleParameters(Parameters.java:706)

However : If i run both the Dataprovider and the method from same class it works fine. Can someone help me. i have changed the classname to make it more meaningful names. Thank you.

One Answer

you are getting the exception because somewhere you are creating a array with negative size

On analyzing the code , the most possible line that causes this issue would be:

Object[][] data = new Object[rows - 1][cols];

In your code you are getting sheet name as :

String sheetName = m.getName();

When you call this from a different class i am not sure whether the class name will be appended along with the method name eg class.M1 ,

So just print row number and sheetName for debugging purpose and see whether this is causing the issue.

if the name "class.M1" is causing the issue , then split the string and get only the method name. Or see if there is other methods that will give only give method name.

Answered by PDHide on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP