0
try {
           tch (IOException e) {   
            System.out.println("Test d");    
        }   

When i run the above code i get below exception.Exception in thread "main"

java.lang.NullPointerException at test.testing.main(testing.java:28)

I am trying to access data from excel and run the test cases.

I am new to java and selenium. How to resolve this issue. please help

Not able to understand where is the mistake in the code...

john
  • 29
  • 9

2 Answers2

1

As I understood, you are trying to read each row's data from the excel file and passing them to runTest(row.getCell(1).toString(),row.getCell(2).toString()); method

so here is your complete code to do that -

try {
        // Open the Excel file
        FileInputStream fis = new FileInputStream("D:\\Workspace\\test\\src\\test\\testdata.xls");
        // Access the required test data sheet
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheet("testdata");
        int rowNumber=0;

         Iterator<Row> rowIterator = sheet.iterator();

            // Traversing over each row of XLSX file
                while (rowIterator.hasNext())
                {                       
                    System.out.println("Running test case "+sheet.getRow(rowNumber).getCell(0).getStringCellValue());
                // Run the test for the current test data row
                runTest(sheet.getRow(rowNumber).getCell(0).toString(),sheet.getRow(rowNumber).getCell(1).toString());

                rowNumber++;
              // To check row cell is not empty
               if(sheet.getRow(rowNumber)==null)
                {
                     break;
                }
            }

         wb.close();
        fis.close();
    } 
catch (IOException e) 
{
        System.out.println("Test data file not found");
   } 
NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

You for should start from 0 and not 1 but you could try using iterator

Adjust following code to your needs

try {
    // Open the Excel file
    FileInputStream fis = new FileInputStream("D:\\Workspace\\test\\src\\test\\testdata.xls");
    // Access the required test data sheet
    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sheet = wb.getSheet("testdata");

    Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) { // cycle throuh all rows
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            if (cellIterator.hasNext()) { //change to while if you need all columns, otherwise if you need the first column leave as it is...
                Cell cell = cellIterator.next();
                System.out.println("Running test case " + cell.getStringCellValue());
            }
        }
    }            
} catch (IOException e) {
    System.out.println("Test data file not found");
}   
finally
{
    if (fis != null)
    fis.close();
}

You can also check the type of a column using:

String tmp = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
    tmp = cell.getNumericCellValue() + "";
    break;
case Cell.CELL_TYPE_STRING:
    tmp = cell.getStringCellValue();
    break;
// case Cell.<other types>: ... ; break;
}
Marcx
  • 6,806
  • 5
  • 46
  • 69
  • Still same error in Iterator rowIterator = sheet.iterator(); – john Dec 22 '16 at 11:26
  • 1
    if your sheet is the first one try using `HSSFSheet sheet = wb.getSheetAt(0);` it should return the first sheet of xls/xlsx... otherwise you are having problem opening/reading the excel file itself... – Marcx Dec 22 '16 at 11:27