尝试写入 excel 中的 null/blank 单元格导致 java.lang.NullPointerException

Try to write to a null/blank cell in excel leads to java.lang.NullPointerException

我正在尝试将 arrayList 的一些元素打印到 excel 中。我使用新创建的 excel 没有内容的文件。

    Row row;
    Cell column;
    int size = records.size(); //records is my ArrayList

    try {
         //Get the excel file.
         FileInputStream file = new FileInputStream(new File("C:\test\test.xls"));
         //Get workbook for XLS file.
         HSSFWorkbook workbook = new HSSFWorkbook(file);
         //Get first sheet from the workbook.
         HSSFSheet sheet1 = workbook.getSheetAt(0);

         for(int i=0; i<size; i++){
                 //here I get the exception
                 row = sheet1.getRow(i+1);  
                 column = row.getCell(0);
                 column.setCellValue(records.get(i).getDate()); // this method returns a date..

            }

            file.close();
            FileOutputStream out =  new FileOutputStream(new File("my_path\test.xls"));                
            workbook.write(out);
            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();    
        }

所以我将这些行替换为

row = sheet1.getRow(i+1);  
if(row.getCell(0)==null){
      column = row.createCell(0);
      column.setCellValue(records.get(i).getDate());
 }else{
       row.getCell(0).setCellValue(records.get(i).getDate()); // this method returns a date..
 }

但是异常还是出现了。然后在我看到这个 question 之后,我尝试了这个

    if(row.getCell(0)==null || getCellValue(row.getCell(0)).trim().isEmpty()){
           column = row.createCell(0);
           column.setCellValue(records.get(i).getDate());
    }else{
           row.getCell(0).setCellValue(records.get(i).getDate());
    }

和方法

private String getCellValue(Cell cell) {
        if (cell == null) {
            return null;
        }
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            return cell.getStringCellValue();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            return cell.getNumericCellValue() + "";
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            return cell.getBooleanCellValue() + "";
        }else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
            return cell.getStringCellValue();
        }else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
            return cell.getErrorCellValue() + "";
        } 
        else {
            return null;
        }
   }

但我仍然遇到异常..

改用.setCellValue("");。如果您尝试设置该值并且它为 null,这意味着无论 POI 正在执行什么操作,它都会尝试 null 值。您也可以不调用单元格上的 .setCellValue()

经过很多天,我找到了问题的根源。问题是当该行为空时......具有以下

row = sheet1.getRow(i+1); 
if(row == null){
  row = sheet1.createRow(i+1);
}

程序运行很好

对于我的情况。我不使用检查单元格类型,而是直接创建单元格 因为我知道我的牢房是空的。这个编辑现有 excel 的问题让我一整天都感到困惑。但是 apache POI 比 jxl 更推荐,只是我的 2 美分

sheet.createRow(newrow);
sheet.getRow(newrow).createCell(3).setCellValue(npk);
sheet.getRow(newrow).createCell(4).setCellValue(tglawal);
sheet.getRow(newrow).createCell(5).setCellValue(tglakhir);
sheet.getRow(newrow).createCell(6).setCellValue(jamawal);
sheet.getRow(newrow).createCell(7).setCellValue(jamakhir);
sheet.getRow(newrow).createCell(8).setCellValue(tujuan);
sheet.getRow(newrow).createCell(9).setCellValue(nomer);

感谢上帝创造了创建 Whosebug 的人和混淆代码的人