Null pointer exception with jexcel while writing

2.1k views Asked by At

I need to append contents to an existing excel file using JExcel.

I am trying the following approach:

  1. Read from existing workbook

    workbook = Workbook.getWorkbook(new File(errorFilePath));
    
  2. 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);
    
  3. Write to writable workbook(times is a writable cell format variable)

    Label label;
    label = new Label(column, row, stringData, times);
    excelSheet .addCell(label);
    
  4. 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

2

There are 2 answers

1
hyde On

Well, first suspicion is, in this line:

label = new Label(column, row, stringData, times);

you pass null argument(s).

0
Nagaraja JB On

I faced the same issue. I was trying to add rows to the sheet dynamically in a loop using insertRow. After spending several hours it was probably a bug in the latest version of jxl api. JXL api after 2.6.9 seem to have bug in insertRow. I switched to 2.6.9 from 2.6.12.