我正在尝试阅读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();
}
}
答案 0 :(得分:-2)
找到它。我已经将excel文件保存在包含类文件的包中,eclipse找不到它......我不知道为什么。所以我将它移动到项目根目录下的一个文件夹中,然后就可以了。