尝试从.xls文件apache poi读取时出现空指针异常

时间:2017-05-09 11:36:00

标签: java selenium automation apache-poi

private static HSSFSheet ExcelWSheet;

private static HSSFWorkbook ExcelWBook;

private static HSSFCell Cell;

private static HSSFRow Row;

private  static String[][] getTableArray(String FilePath, String SheetName)    throws Exception

{   

    FileInputStream ExcelFile = null;
    String[][] tabArray = null;

   try{

       ExcelFile = new FileInputStream(FilePath);

       // Access the required test data sheet

       ExcelWBook = new HSSFWorkbook(ExcelFile);

       ExcelWSheet = ExcelWBook.getSheet(SheetName);

       int startCol = 1;

       int ci=0;

       int totalRows = ExcelWSheet.getPhysicalNumberOfRows();

       int totalCols = ExcelWSheet.getRow(1).getLastCellNum(); 
       System.out.println(totalRows);

       System.out.println(totalCols);
        tabArray=new String[totalRows][totalCols];
        DataFormatter formatter = new DataFormatter();
           for (int j=1;j<=totalRows;j++, ci++)

           {
               //totalCols = ExcelWSheet.getRow(j).getPhysicalNumberOfCells();
               //tabArray=new String[totalRows][totalCols];
               int cj=0;
               for (int l = 1; l < totalCols;l++, cj++) {
                //tabArray[ci][cj] = getCellData(j, l);
                 Cell = ExcelWSheet.getRow(j).getCell(l);

                 String CellData = formatter.formatCellValue(Cell);
                 System.out.println(CellData);

                 tabArray[ci][cj] = CellData;

                System.out.println(j);
                System.out.println(l);
                System.out.println(tabArray[ci][cj]);
                System.out.println("tabArray["+ci+"]["+cj+"]: "+tabArray[ci][cj]);
            }


           }
           System.out.println(tabArray);
    }

    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();

    }finally {

        ExcelFile.close();
    }

    return tabArray;

}

///// Cell = ExcelWSheet.getRow(j).getCell(l);这里抛出空指针异常。

coloumns标题是TestCases,Inquiry ID,Sl No.,Material

只需从第二列查询ID读取并在数据驱动框架中逐一传递。

想要从excel读取查询ID并将其传递给我的@test方法

0 个答案:

没有答案