0

I am trying to access the getrownum() from the main method, however NullPointerException is thrown.

Main class:

public class MainDriveScenario {

    public static void main(String args[]) throws Exception {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestCaseExecSheet, Constant.Sheet1_TestCaseExecSheet);

        int rownum = ExcelUtils.getrownum(Constant.Sheet1_TestCaseExecSheet);

        for (int i = 1; i < rownum + 1; i++) {
            String exec = ExcelUtils.getCellData(i, 2);
            System.out.println(exec);
            String testcaseID = ExcelUtils.getCellData(i, 0);
            System.out.println(testcaseID);

        }
    }

}

ExcelUtil class:

 public class ExcelUtils {

    public static void setExcelFile(String Path,String SheetName) throws Exception {

            try {

                // Open the Excel file

            FileInputStream ExcelFile = new FileInputStream(Path);

            // Access the required test data sheet

            ExcelWBook = new XSSFWorkbook(ExcelFile);

            ExcelWSheet = ExcelWBook.getSheet(SheetName);

            } catch (Exception e){

                throw (e);

            }

    }

public static int getrownum(String Sheetname) {
            int rownum = ExcelWSheet.getLastRowNum();
            return rownum;
        }
    }
viki
  • 11
  • 1
  • 7
  • Have you tried debugging? Where does the NPE occur? – DerMike Jan 15 '16 at 12:56
  • In excelUtil class ExcelWSheet object could be null. – bond007 Jan 15 '16 at 12:57
  • I can't see any `setExcelFile` or `getCellData` function in the `ExcelUtils` class you have mentioned. – Sendhilkumar Alalasundaram Jan 15 '16 at 12:59
  • @SendhilkumarAlalasundaram I update the question with required methods, not sure why in main method I am getting exception. – viki Jan 15 '16 at 13:06
  • @DerMike Yes mike i tried dubugging and found exception is happening at public static int getrownum(String Sheetname) { int rownum=ExcelWSheet.getLastRowNum(); //System.out.println("the last row num is "+rownum); return rownum; } – viki Jan 15 '16 at 16:20

0 Answers0