I need to append contents to an existing excel file using JExcel.
I am trying the following approach:
Read from existing workbook
workbook = Workbook.getWorkbook(new File(errorFilePath));
Create writable workbook from exisitng workbook into a temp file
if (!tempFile.exists()) { tempFile.getParentFile().mkdirs(); tempFile.createNewFile(); } newCopy = Workbook.createWorkbook(tempFile, workbook); excelSheet = newCopy.getSheet(0);
Write to writable workbook(times is a writable cell format variable)
Label label; label = new Label(column, row, stringData, times); excelSheet .addCell(label);
Close both exisitng and writable workbook->Delete exisitng workbook in finally block -> Rename temp file name to existing(now deleted) workbook name
finally { if (null != newCopy) { newCopy.write(); newCopy.close(); } if (null != workbook) { workbook.close(); } if (null != errorFile && errorFile.exists()) { errorFile.delete(); } if (null != tempFile) { tempFile.renameTo(new File(errorFilePath)); } }
The problem is everything works fine for the first run(without redeploying). But whenever I change some java code, and the web application redeploys I get a null pointer exception while closing the newly created workbook(after writing).
I am getting the following stack trace(originating from line newCopy.write())
java.lang.NullPointerException
at jxl.write.biff.CellValue.getData(CellValue.java:259)
at jxl.write.biff.LabelRecord.getData(LabelRecord.java:141)
at jxl.biff.WritableRecordData.getBytes(WritableRecordData.java:71)
at jxl.write.biff.File.write(File.java:147)
at jxl.write.biff.RowRecord.writeCells(RowRecord.java:329)
at jxl.write.biff.SheetWriter.write(SheetWriter.java:479)
at jxl.write.biff.WritableSheetImpl.write(WritableSheetImpl.java:1514)
at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:950)
Java Version : 1.6
JExcel Version : 2.6.10
Windows 7
Well, first suspicion is, in this line:
you pass
null
argument(s).