0

I have created data in an excel sheet. I want data from two sheets (i.e Sheet1 and Sheet2)

So I have written two data providers as below. I have merged both dataproviders. But i'm getting Null pointer exception. I'm not sure why it is throwing null pointer exception.

@DataProvider
public  Object[][] getStep1TestData()

{
    Object data1[][]= TestUtil.getTestData(sheet1);
    return data1;
}


@DataProvider
public  Object[][] getStep2TestData()

{
    Object data2[][]= TestUtil.getTestData(sheet2);// ---getting 

null pointer exception return data2; }

@DataProvider
public Object mergeData()

{
      List<Object[]> result = Lists.newArrayList();
      result.addAll(Arrays.asList(getStep1TestData()));
      result.addAll(Arrays.asList(getStep2TestData()));// ----getting null pointer exception here
      return result.toArray(new Object[result.size()][]);
}

//@DataProvider
@Test(dataProvider="mergeData")
public void validateStep2Page(String customer, String customertype, String iscontractsigned, String productname,String productamount,String locationname,
        String address1,String address2,String city, String state, String postalcode, String county, String country, 
        String daysofservices, String servicebranchcode, String csvbranchcode, String product, String quantity, 
        String changeorder) throws Exception

{
    salescreatecontractstep1Page.Selectcustomer(customer, customertype, iscontractsigned, productname, productamount);
    salescreatecontractstep2Page.addlocation(locationname, address1, address2, city, state, postalcode, county, country, daysofservices, servicebranchcode, csvbranchcode, product, quantity, changeorder);
}
Aksh K
  • 1
  • 1
  • Does this answer your question? [TestNG: More than one @DataProvider for one @Test](https://stackoverflow.com/questions/10832422/testng-more-than-one-dataprovider-for-one-test) – Gautham M Jun 22 '21 at 15:50
  • Tried with the approach but getting null pointer exception https://stackoverflow.com/questions/10832422/testng-more-than-one-dataprovider-for-one-test – Aksh K Jun 23 '21 at 13:35
  • Maybe you could add the code you have tried to this question. – Gautham M Jun 23 '21 at 14:20
  • `TestUtil.getTestData(sheet2);` are you sure that this is not returning `null`? If it is returning `null` then `Arrays.asList()` would throw a null pointer exception. You may return `new Object[][]` in `getStep1TestData` and `getStep2TestData` if the `TestUtil.getTestData` returns a `null`. – Gautham M Jun 24 '21 at 11:47
  • Yes TestUtil.getTestData(sheet2); is throwing NPE. Can you let me know how to return new object in getStep1TestData and getStep2TestData ? – Aksh K Jun 28 '21 at 05:37
  • After invoking `getTestData`, Just do a `null` check and return. `return (data1 == null) ? new Object[][]{} : data;` – Gautham M Jun 28 '21 at 05:43
  • Also, I would suggest you to close this question as a duplicate as the linked duplicate solves your question on how to combine two dataproviders. NPE is a different issue and not related to the original question of merging data providers. – Gautham M Jun 28 '21 at 05:45

0 Answers0