2

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 Data provider and the method from same class it works fine.

I have changed the classname to make it more meaningful names.

6
  • Please share your sheet data with sheet name and its data Jul 21, 2020 at 6:43
  • what module are you suing to get excel POI?
    – PDHide
    Jul 21, 2020 at 7:14
  • i got it . Thank you for your time. Jul 22, 2020 at 7:42
  • Please close the request. i got the answer. Jul 22, 2020 at 7:42
  • If the other answer solved your problem, please accept that answer as the correct one. If not, please post what actually solved the problem and accept that answer as correct. (sqa.stackexchange.com/help/someone-answers)
    – Kate Paulk
    Jul 22, 2020 at 11:35

1 Answer 1

1

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.

1
  • Please accept the answer by clicking the tick sign near to my answer, if it helped
    – PDHide
    Jul 22, 2020 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.