testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.98k stars 1.02k forks source link

[Utils] [ERROR] [Error] org.testng.TestNGException: Data Provider public static java.lang.Object[][] returned a null value #1950

Closed Hari-Shetty closed 5 years ago

Hari-Shetty commented 5 years ago

Please let me know where i'm going wrong.

TestNG 6.14.2 Java1.8 Maven 3.5.4 selenium 3.14.2 poi 3.17

----------------BaseClass.java-----------------------------

package resources;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class BaseClass {

 public WebDriver driver;
 public static Properties prop;

 public WebDriver initializeDriver() throws IOException {
  prop = new Properties();
  FileInputStream fis = new FileInputStream("\\Users\\Vineet\\AutomationFramework\\src\\main\\java\\resources\\data.properties");
  prop.load(fis);
  String browser = prop.getProperty("browser");
  //String url = prop.getProperty("url");
  System.out.println(browser);

  if (browser.equals("Chrome")) {
   System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\driver\\chromedriver.exe");
   driver = new ChromeDriver();
  } else if (browser.equals("Firefox")) {
   System.setProperty("webdriver.gecko.driver", "E:\\gecko 0.18\\geckodriver.exe");
   driver = new FirefoxDriver();
  } else if (browser.equals("IE")) {
   System.setProperty("webdriver.ie.driver", "E:\\Selenium\\driver\\iedriver.exe");
   driver = new InternetExplorerDriver();
  }

  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  return driver;
 }
}

------------GetExcelData.java------------------

package resources;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
//import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.testng.annotations.Test;
//import org.testng.annotations.Guice;

public class GetExcelData {
 public static XSSFSheet ExcelWSheet;
 public static XSSFWorkbook ExcelWBook;
 public static XSSFCell Cell;

 public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {
  String[][] tabArray = null;

  try {
   FileInputStream ExcelFile = new FileInputStream(FilePath);
   // Access the required test data sheet
   ExcelWBook = new XSSFWorkbook(ExcelFile);
   ExcelWSheet = ExcelWBook.getSheet(SheetName);
   int startRow = 1;
   int startCol = 1;

   int ci, cj;
   int totalRows = ExcelWSheet.getLastRowNum();
   // you can write a function as well to get Column count
   int colcount = ExcelWSheet.getRow(startRow).getPhysicalNumberOfCells();
   int totalCols = colcount - 1;
   System.out.println(totalCols);

   tabArray = new String[totalRows][totalCols];
   ci = 0;

   for (int i = startRow; i <= totalRows; i++, ci++) {
    cj = 0;
    for (int j = startCol; j <= totalCols; j++, cj++) {
     tabArray[ci][cj] = getCellData(i, j);
     System.out.println(tabArray[ci][cj]);
    }
   }

  } catch (FileNotFoundException e) {
   System.out.println("Could not read the Excel sheet");
   e.printStackTrace();
  } catch (IOException e) {
   System.out.println("Could not read the Excel sheet");
   e.printStackTrace();
  }
  return (tabArray);
 }

 public static String getCellData(int RowNum, int ColNum) throws Exception {
  try {
   DataFormatter df = new DataFormatter();
   Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);

   CellType dataType = Cell.getCellTypeEnum();
   String celldata;
   if (dataType == CellType.NUMERIC) {
    celldata = df.formatCellValue(Cell);
   } else {
    celldata = Cell.getStringCellValue();
   }
   return celldata;
  } catch (Exception e) {
   System.out.println(e.getMessage());
   throw (e);
  }
 }
}

-------------------RegisterTest.java-----------------------------------


package mypack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import pageObjects.RegisterPage;
import resources.BaseClass;
//import resources.ExcelData;
import resources.GetExcelData;

//import resources.*;

@Guice
public class RegisterTest2 extends BaseClass {

 public static Object[][] str;
 RegisterPage reg = new RegisterPage(driver);

 @Test(dataProvider = "FormData")
 public void register(String first, String last, String addr, String email, String phone, String skills, String country1, String country2, String year, String month, String day, String pass, String conpass) throws IOException {
  driver = initializeDriver();
  driver.get(prop.getProperty("url"));

  Random random = new Random();
  int randomInt = 0;
  for (int idx = 100; idx <= 1000; ++idx) {
   randomInt = random.nextInt(1000);
   //System.out.println("Generated : " + randomInt);
  }

  reg.firstname().sendKeys(first + randomInt);
  reg.lastname().sendKeys(last + randomInt);
  reg.address().sendKeys(addr + randomInt);
  reg.email().sendKeys(email);
  reg.tel().sendKeys(randomInt + phone);

  JavascriptExecutor js = (JavascriptExecutor) driver;
  js.executeScript("window.scrollBy(0,500)", "");

  ArrayList < WebElement > gender = (ArrayList < WebElement > ) reg.gender();

  int index = random.nextInt(gender.size());
  gender.get(index).click();

  ArrayList < WebElement > hobby = (ArrayList < WebElement > ) reg.hobby();
  for (WebElement chk: hobby) {
   if (!chk.isSelected()) {
    chk.click();
   }
  }

  reg.skills().sendKeys(skills);
  reg.country().sendKeys(country1);
  //reg.sel_country().sendKeys(country2);
  reg.year().sendKeys(year);
  reg.month().sendKeys(month);
  reg.day().sendKeys(day);
  reg.pwd().sendKeys(pass);
  reg.conpwd().sendKeys(conpass);
  //reg.submit().click();       
 }

 @DataProvider
 public static Object[][] FormData() {
  try {
   //System.out.println(ex.getTableArray(prop.getProperty("filepath"), prop.getProperty("sheetname")));
   str = GetExcelData.getTableArray(prop.getProperty("filepath"), prop.getProperty("sheetname"));
   /*for(Object o: str) {
    System.out.println("data is" +o);
   }*/
  } catch (Exception e) {
   System.out.println(e);
  }
  return (str);
 }
}

-------------ERROR TRACE-------------------

[RemoteTestNG] detected TestNG version 6.14.2
java.lang.NullPointerException
[Utils] [ERROR] [Error] org.testng.TestNGException: 
Data Provider public static java.lang.Object[][] mypack.RegisterTest2.FormData() returned a null value
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:133)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:923)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: register
org.testng.TestNGException: 
Data Provider public static java.lang.Object[][] mypack.RegisterTest2.FormData() returned a null value
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:133)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:923)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
krmahadevan commented 5 years ago

Please post questions to the TestNG google forums.

As the error says Data Provider public static java.lang.Object[][] mypack.RegisterTest2.FormData() returned a null value, your data provider is basically returning a null value. You would need to debug your data provider viz., FormData() method and check if its returning values properly. TestNG is working as designed in this case, and I dont think this is a TestNG issue, but more of a problem with your test code.

PS: The latest released version of TestNG is 7.0.0-beta1. Please ensure that you are using this version.

Closing this issue with resolution as Question answered