检索Excel单元的内容返回NullPoInterException



我正在尝试阅读Excel工作表的内容,但是我的方法一直在抛出一个例外。我已经尝试了所有事情,但我敢肯定,这是我看不到的非常小的细节。有人可以用一双新鲜的眼睛看我的代码,并可能指出我做错了什么。谢谢!

 /**
 * This method sets the path to the excel file and the excel sheet as well as
 * initializing the stream
 */
 public static void setExcelFile(String path, String sheetName) {
    try {
        FileInputStream excelFile = new FileInputStream(path);
        workbook = new XSSFWorkbook(excelFile);
        worksheet = workbook.getSheet(sheetName);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
 }

 /**
 * This method reads the cell data from the excel file
 */
public static String getCellData(int rowNum, int colNum) {
    try {
        //This is what is causing my nullpointerexception according to eclipse
        row = worksheet.getRow(rowNum);
        //cell = worksheet.getRow(rowNum).getCell(colNum);
        cell = row.getCell(colNum);
        String cellData = cell.getStringCellValue();
        return cellData;
    }
    catch (Exception e) {
        e.printStackTrace();
        return "Lactoferrin";
    }
}

/**
 * This is how i call both methods
 */
public static void main(String[] args) {
    try {
        ExcelUtility.setExcelFile(PATHTOTESTDATA, FILENAME);
        String cellContents = ExcelUtility.getCellData(1,0);
        System.out.println(cellContents);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

找到了它。我将Excel文件保存在包含类文件的软件包中,而Eclipse找不到它...我不知道为什么。因此,我将其移到了项目根部的文件夹中,并且可以工作。

最新更新