java - 尝试写入 excel 中的空/空白单元格会导致 java.lang.NullPointerException

标签 java apache-poi

我正在尝试将 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;
        }
   }

但我仍然遇到异常..

最佳答案

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

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

程序运行良好。

关于java - 尝试写入 excel 中的空/空白单元格会导致 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32506580/

相关文章:

java - Android java,从位置N和N之间的列表中获取所有值

持续时间的 Java pretty-print

java - Jsoup登录爬取游戏数据

java - Spring Autowiring 依赖,具有构造函数参数

java - 使用 Apache POI 从 Java 中读取 Excel 的公式字段

java - 使用 maven 的 Web 片段

java - Apache POI 相对文件超链接

java - 如何使用 apache poi 在文档 .docx 中的表格单元格中创建文本框

java - EXcel 表 POI 验证 : Out Of Memory Error

java - 将视频文件 (.mp4) 集成到 docx/doc 中