我正在尝试将信息附加到现有的 Excel 文件,但我不断收到 java.lang.NullPointerException。请参阅下面的代码和异常消息。
代码:
private WritableSheet appendingSheet;
private static File report;
public void AppendToDoc (String path) throws IOException, WriteException, BiffException {
this.inputFile = path;
report = new File(inputFile);
Workbook appendingWorkbook = Workbook.getWorkbook(new File(inputFile));
WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"),appendingWorkbook);
appendingSheet = copy.getSheet("Sheet 1");
}
public void WriteToDoc (int option, String testName, String execDate, String time, boolean status) throws RowsExceededException, WriteException, IOException, BiffException{
int startingRow = NumOfRows(this.inputFile) + 1; //I have a function which finds the number of existing rows in the existing document. It works.
Label label = new Label(1, startingRow, "hello", times);
appendingSheet.addCell(label);
}
调用代码:
AppendToDoc("C:/Users/smith/ExcelTestFile.xls");
WriteToDoc(2, "This is a test", "executed", timeStamp, true);
异常消息:
jxl.write 的 jxl.write.biff.CellValue.addCellFormat(CellValue.java:468) 的 jxl.write.biff.Styles.getFormat(Styles.java:214) 的线程“main”java.lang.NullPointerException 中的异常。 biff.CellValue.setCellDetails(CellValue.java:282) 在 jxl.write.biff.LabelRecord.setCellDetails(LabelRecord.java:216) 在 jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:1199) 在 ExcelOperations.WriteToDoc (ExcelOperations.java:149) 在 ChequeImage.main(ChequeImage.java:174)
注意:异常似乎发生在“appendingSheet.addCell(label);”行。
谢谢您的帮助。
编辑 1:下图显示了 appendingSheet 在执行“appendingSheet.addCell(label)”之前的内容。我不确定它代表什么,但它确实似乎并不为空。