写入工作簿时获取java.lang.NullPointerException WorkBook.write(out)Apache POI

写入输出stream时出现java.lang.NullPointerException

 workbook.write(new FileOutputStream("test1.xslx")); 

例外是:

 Exception: java.lang.NullPointerException at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:201) at ExcelCompare.WriteOutputIntoExcel.addRow(WriteExcel.java:124) at ExcelCompare.CompareExcel.main(MainClassExcelCompare.java:113) 

这里我有两个类: CompareExcel类和WriteOutputIntoExcel

我想比较两个Excel表格Excel1.xslxExcel2.xslx ,并将结果放回到Result.xslx

我不想把所有的东西都放在Result.xslx ,我只是想放置在Excel1Excel2.都不匹配的行Excel2.

这是Main

 package ExcelCompare; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CompareExcel { public static void main(String[] args) { try { int temp; File excel1 = new File("C://Users/ckothakapax076037/Desktop/Demo1.xlsx"); FileInputStream fis1 = new FileInputStream(excel1); XSSFWorkbook book1 = new XSSFWorkbook(fis1); XSSFSheet sheet1 = book1.getSheetAt(0); File excel2 = new File("C://Users/ckothakapax076037/Desktop/Demo2.xlsx"); FileInputStream fis2 = new FileInputStream(excel2); XSSFWorkbook book2 = new XSSFWorkbook(fis2); XSSFSheet sheet2 = book2.getSheetAt(0); WriteExcel obj1 = new WriteExcel(); obj1.setOutputFile("C://Users/ckothakapax076037/Desktop/Result.xlsx"); //Get iterator to all the rows in current sheet Iterator<Row> itr1 = sheet1.iterator(); Iterator<Row> itr2 = sheet2.iterator(); // Iterating through all cells row by row while (itr1.hasNext() && itr2.hasNext()) { temp = 0; int j = 0; Row row1 = itr1.next(); Row row2 = itr2.next(); //Get iterator to all cells of current row Iterator<Cell> cellIterator1 = row1.cellIterator(); Iterator<Cell> cellIterator2 = row2.cellIterator(); CellStyle style = book1.createCellStyle(); style = book1.createCellStyle(); style.setFillForegroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); while (cellIterator1.hasNext() && cellIterator2.hasNext()) { Cell cell1 = cellIterator1.next(); Cell cell2 = cellIterator2.next(); switch (cell1.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell1.getStringCellValue() + "\t"); System.out.print(cell2.getStringCellValue() + "\t"); if (!cell1.getStringCellValue().equalsIgnoreCase(cell2.getStringCellValue())) { temp++; cell1.setCellStyle(style); } break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell1.getNumericCellValue() + "\t"); System.out.print(cell2.getNumericCellValue() + "\t"); if (cell1.getNumericCellValue() != cell2.getNumericCellValue()) { temp++; cell1.setCellStyle(style); } break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell1.getBooleanCellValue() + "\t"); System.out.print(cell2.getBooleanCellValue() + "\t"); break; case Cell.CELL_TYPE_BLANK: System.out.print(cell1.getNumericCellValue() + "\t"); System.out.print(cell2.getNumericCellValue() + "\t"); if (cell2.getStringCellValue() != " ") { temp++; cell1.setCellStyle(style); } break; default: } j++; } System.out.print("\n"); System.out.print("Flag value:" + temp); System.out.print("\n"); if (temp >= 1) { obj1.addRow(row1.cellIterator(), row2.cellIterator()); } } book1.close(); fis1.close(); book2.close(); fis2.close(); obj1.closerActivity(); } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (IOException ie) { ie.printStackTrace(); } catch (Exception ee) { ee.printStackTrace(); } } } 

现在我有WriteOutputIntoExcel类将结果放回到Result.xslx

 package ExcelCompare; import java.io.FileOutputStream; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteOutputIntoExcel { private static String OutputFile; private static XSSFWorkbook myWorkBook = new XSSFWorkbook(); private static XSSFSheet mySheet = myWorkBook.createSheet("Report"); public static int i = 0; public void setOutputFile(String OutputFile1) { OutputFile = OutputFile1; } public void addRow(Iterator<Cell> cellIterator1, Iterator<Cell> cellIterator2) { try { XSSFRow row = mySheet.createRow(i++); CellStyle style = myWorkBook.createCellStyle(); style = myWorkBook.createCellStyle(); style.setFillForegroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); FileOutputStream out = new FileOutputStream(OutputFile); System.out.print("Writing result from Sheet1"); System.out.print("\n"); while (cellIterator1.hasNext()) { int j = 0; Cell cell1 = cellIterator1.next(); switch (cell1.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell1.getStringCellValue() + "\t"); row.createCell(j).setCellValue(cell1.getStringCellValue()); row.createCell(j).setCellStyle(style); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell1.getNumericCellValue() + "\t"); row.createCell(j).setCellValue(cell1.getNumericCellValue()); row.createCell(j).setCellStyle(style); break; case Cell.CELL_TYPE_BLANK: System.out.print(cell1.getStringCellValue() + "\t"); row.createCell(j).setCellValue(cell1.getStringCellValue()); row.createCell(j).setCellStyle(style); break; default: System.out.print(cell1.getStringCellValue() + "\t"); row.createCell(j).setCellValue(cell1.getStringCellValue()); row.createCell(j).setCellStyle(style); } j++; } System.out.print("\n"); System.out.print("Writing result from Sheet2"); System.out.print("\n"); while (cellIterator2.hasNext()) { int j = 0; Cell cell2 = cellIterator2.next(); switch (cell2.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell2.getStringCellValue() + "\t"); row.createCell(j).setCellValue(cell2.getStringCellValue()); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell2.getNumericCellValue() + "\t"); row.createCell(j).setCellValue(cell2.getNumericCellValue()); break; case Cell.CELL_TYPE_BLANK: System.out.print(cell2.getStringCellValue() + "\t"); row.createCell(j).setCellValue(cell2.getStringCellValue()); break; default: System.out.print(cell2.getStringCellValue() + "\t"); row.createCell(j).setCellValue(cell2.getStringCellValue()); } j++; } System.out.print("\n"); myWorkBook.write(out); out.close(); myWorkBook.close(); } catch (Exception e) { e.printStackTrace(); } } public void closerActivity() { try { System.out.println(" Hi i am in close"); } catch (Exception e) { e.printStackTrace(); } } } 

我也想突出Excel1中与Excel2的单元格。

请帮我做到这一点。

解决这类问题的最好方法是将库的源代码(在这种情况下是POI)附加到IDE中,以便快速查找库中201行的情况。 请注意,您需要确保您的库lib版本与src版本相同,否则可能会看到错误的行。

好的,最有可能你addRow的一个参数是null(迭代器)。 所以首先检查(println)。