編輯:我正在使用此代碼來讀取excel表的行和列。但標有雙星的行引發異常(java.lang.nullpointer異常)。此外,我也想轉換在兩欄之間找到空列,空欄。請幫助。行= sheet.getRow(rowNumber)即使在Excel工作表中存在行時也會返回java.lang.nullpointer異常。我正在使用Apache POI
有2個循環一個用於行和內部用於列的明顯。 Niehter該行正在讀取,也沒有空白單元格問題得到解決。
public void read(String filePath) throws NullPointerException {
try {
FileInputStream file = new FileInputStream(new File(filePath));
System.out.println("going to create Workbook object to read excel row wise");// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
System.out.println("going to create object sheet to read excel row wise");
XSSFSheet sheet = workbook.getSheetAt(0); //extracts the first sheet of excel
System.out.println("Running row iterator to read excel row wise");
for (int rowNumber = sheet.getFirstRowNum(); rowNumber <= sheet.getLastRowNum(); rowNumber++) {
if (rowNumber == sheet.getFirstRowNum())// this row was skipped as it was header row.
{
System.out.println("skipping first row");
continue;
}
this.rowconcat = ""; // all values of perticular row will be concatinated in this variable.
this.flag = 0;
if (sheet.getRow(rowNumber) == null) {
System.out.println("row detected null");
} else {
// The row has data
System.out.println("row no inside else==="+rowNumber);
**Row row = sheet.getRow(rowNumber);** //This line here throws null pointer exception.
for (int cellNumber = row.getFirstCellNum(); cellNumber <= row.getLastCellNum(); cellNumber++) {
Cell cell = row.getCell(cellNumber);
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
this.flag = 1; // set to 1 to indicate blank cell but this also doesn't work
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(" "); //inserting space in cell after converting its type to String.
this.rowconcat = this.rowconcat + cell.getStringCellValue() + "~";
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
this.rowconcat = this.rowconcat + cell.getStringCellValue() + "~";
}
}
}
}
}//try block closure
catch(Exception e){
//autogenerated
}
} // function closure
你確定這就是拋出異常的行?這對我來說似乎不可能。 –
非常確定。實際上,我打印在它上面的線顯示在日誌中,並立即在它閃爍空例外 –
但是,是在棧跟蹤中提到的線? –