0

I am trying to read data from excel using apache poi with testng data provider but testng is skipping my test method.

Below is the error am getting

SKIPPED: VerifyLoanDetails
java.lang.RuntimeException: java.lang.NullPointerException

Caused by: java.lang.NullPointerException at com.seleniumhybrid.utils.ExcelUtil.getTestdata(ExcelUtil.java:62) at com.seleniumdata.zmartano.LoanDetails.getdata(LoanDetails.java:50)

Below is my class to read data from excel

public class ExcelUtil{

    static Workbook book;
    static Sheet  sheet;
    public static String Testdata_sheet = Constants.path_TestData;



    public static Object  [][] getTestdata(String sheetname) {



        FileInputStream file = null;
        try{
        file = new FileInputStream(Testdata_sheet);
        }
        catch(FileNotFoundException e) {

            e.printStackTrace();
        }


        try {

            book = WorkbookFactory.create(file);
        }
        catch(InvalidFormatException e ) {

            e.printStackTrace();
        }

        catch (IOException e) {

            e.printStackTrace();

        }


        sheet = book.getSheet(sheetname);

        Object [][]data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
        for (int i=0;i<sheet.getLastRowNum();i++) {

            for(int k=0;k<sheet.getRow(0).getLastCellNum();k++) {

                data[i][k]= sheet.getRow(i+1).getCell(k).toString();

            }


        }

        return data;



    }
}

below is my test class to acess the data

public class LoanDetails extends Configreader{



                  WebDriver driver;     

   @BeforeTest
        public  void beforetest() throws Exception { 

                  driver = Browser.GetBrowser();




       }






    @Test(dataProvider = "getdata")

   public  void VerifyLoanDetails(String username) throws Exception {


    driver.findElement(By.xpath(pro.getProperty("account_xpath"))).click();
    driver.findElement(By.xpath(pro.getProperty("username_id"))).sendKeys(username);


    }


    @DataProvider
    public Object[][] getdata(){

        Object data [][] = ExcelUtil.getTestdata("credentials");


        return data;


    }
}
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
jino
  • 69
  • 1
  • 12
  • In what line the exception is thrown? is there any caught exception in `getTestdata()`? I suggest you debug it and see what happens. – Guy Aug 27 '18 at 07:09
  • @Guy its showing error in Caused by: java.lang.NullPointerException at com.seleniumhybrid.utils.ExcelUtil.getTestdata(ExcelUtil.java:62) at com.seleniumdata.zmartano.LoanDetails.getdata(LoanDetails.java:50) – jino Aug 27 '18 at 08:15
  • 1
    The exception is somewhere in `getTestdata()`. You need to debug this method and see what happens, or remove all the `try catch` blocks from there to see where is the problem. I bet those blocks mask the real problem, and no much point in them any way, the test can't run without this data anyway, handeling the exception won't help. – Guy Aug 27 '18 at 08:33
  • There is no issue with TestNG. Please check with your POI wrapper that you have written. Seems like the poi wrapper didn't bring any data to the data provider. Check on variable initialization and value type in the excel. – Kshetra Mohan Prusty Aug 27 '18 at 10:15
  • @Amit can U have a look – jino Aug 27 '18 at 12:41
  • @Guy not working , can u have a look – jino Aug 28 '18 at 04:55
  • @jino There isn't something obvious in the code, you will have to debug it. See my previous comment. – Guy Aug 28 '18 at 05:22
  • @jino Make sure by retrieving Data Provider return value, Print value of "data" in console. Also, you need to make sure your excel read code whether its retrieves values from excel. First create simple Java program and check it, and once everything works separately implement it with TestNG annotation. – Ishita Shah Aug 28 '18 at 05:24
  • @IshitaShah print not working , can u have a look – jino Aug 28 '18 at 07:00
  • You have to print like System.out.println(data); – Ishita Shah Aug 28 '18 at 07:04
  • @IshitaShah yes i did bt not printing – jino Aug 28 '18 at 07:15
  • @jino So you have problem with your excel read code. – Ishita Shah Aug 28 '18 at 07:57
  • @IshitaShah ok, let me check it , if you able to find it, plz let me know – jino Aug 28 '18 at 08:34
  • @IshitaShahmy tesng is skipping that test "Verify loan details" – jino Aug 28 '18 at 08:58

0 Answers0