gpt4 book ai didi

将信息附加到现有 Excel 文件时出现 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 04:30:59 25 4
gpt4 key购买 nike

我正在尝试将信息附加到现有 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);

异常消息:

Exception in thread "main" java.lang.NullPointerException at jxl.write.biff.Styles.getFormat(Styles.java:214) at jxl.write.biff.CellValue.addCellFormat(CellValue.java:468) at jxl.write.biff.CellValue.setCellDetails(CellValue.java:282) at jxl.write.biff.LabelRecord.setCellDetails(LabelRecord.java:216) at jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:1199) at ExcelOperations.WriteToDoc(ExcelOperations.java:149) at ChequeImage.main(ChequeImage.java:174)

注意:异常似乎发生在“appendingSheet.addCell(label);”行。

感谢您的帮助。

编辑1:下图显示了执行“appendingSheet.addCell(label)”之前appendingSheet的内容。我不太确定它代表什么,但它看起来肯定不为空。 enter image description here

最佳答案

我不能说我熟悉 Biff,但 POI 似乎以类似的方式工作。看起来您已经创建了一个工作簿:

WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"),appendingWorkbook);

但是您不会创建引用的工作表:

appendingSheet = copy.getSheet("Sheet 1");

我猜你会得到一个 NPE,因为“copy.getSheet("Sheet 1");”返回 null。与您简单地在 Excel 中打开工作簿不同,API 可能不会为您创建默认工作表。我敢打赌,您必须先创建工作表对象,然后才能添加单元格。

关于将信息附加到现有 Excel 文件时出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17979749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com