java.lang.NullPointerException when appending information to an existing Excel file

余生长醉 提交于 2021-01-28 10:33:52

问题


I'm trying to append information to an existing Excel file, but I keep receiving java.lang.NullPointerException. Please refer to the code and exception message below.

Code:

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);

}

Calling code:

AppendToDoc("C:/Users/smith/ExcelTestFile.xls");
WriteToDoc(2, "This is a test", "executed", timeStamp, true);

Exception message:

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)

NOTE: The exception seems to be happening at the line "appendingSheet.addCell(label);".

Thanks for the help.

EDIT 1: The below image shows the contents of appendingSheet right before "appendingSheet.addCell(label)" is executed. I'm not exactly sure what it represents, but it certianly doesn't seem to be null. enter image description here


回答1:


I can't say I'm familiar with Biff, but POI seems to work in a similar fashion. It looks as though you have created a workbook:

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

But then you don't create the sheet that you reference with:

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

I'm guessing you're getting an NPE because "copy.getSheet("Sheet 1");" is returning null. Unlike when you simply open a workbook in excel, the API will likely not create that default sheet for you. I'm betting you'll have to create the sheet object before you're able to add cells.




回答2:


So...I've managed to solve the problem.

I can't really explain why, but after I redefined "copy" and "appendingWorkbook" (not appendingSheet) as global variables instead of local to AppendToDoc(); as well as added the line copy.write(), the program worked...

Thanks everyone for the help. I really appreciate it.




回答3:


I have face the following Exception while writing the data into an XLS file using JXL API.

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 com.java.report.XLS_JXLReport.main(XLS_JXLReport.java:31)

I have observed the following error is occurring due to WritableFont is empty. Source code from where the exception is raised is:

Styles.java:214
    // Do the same with the statically shared fonts
    if (format.getFont() == WritableWorkbook.ARIAL_10_PT) // Line 214
    {
      format.setFont(getArial10Pt());
    }

To avoid this error make sure to provide the Font Name.

Sample code using Java Excel API » 2.6.12

public class XLS_JXLReport {
    static String filePath = "C:/Yash/";
    public static void main(String[] args) throws IOException, JXLException {
        //Creates a writable workbook with the given file name
        jxl.write.WritableWorkbook workbook = jxl.Workbook.createWorkbook(new File(filePath+"CopyCell.xls"));
        WritableSheet sheet = workbook.createSheet("My Sheet", 0);
        
        // Create cell font and format
        WritableFont cellFont = new jxl.write.WritableFont(WritableFont.TIMES, 16);
            cellFont.setColour(jxl.format.Colour.BLUE);
        WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
            cellFormat.setBackground(jxl.format.Colour.GRAY_25);
        
        //cellFormat = null; // To get the NullPointerException at Styles.java:214. Uncomment this line of code.
        
        // Create the label, specifying content and format
        Label label = new Label(1, 2, "ABCD", cellFormat);
        sheet.addCell(label);
        
        //Create copies of cell in a loop
        WritableCell copiedCell = null;
        for (int i = 0 ; i < 4 ; i ++) {
            copiedCell = label.copyTo(1, 4 + i); //Deep copy cell
            sheet.addCell(copiedCell);
        }
        
        closeWorkbook(workbook);
    }
    public static void closeWorkbook(WritableWorkbook workbook) throws IOException, JXLException {
        if (workbook == null)
            return;
        if (workbook.getNumberOfSheets() == 0) {
            workbook.createSheet("No data", 0); // otherwise pointer error
        }
        //Writes out the data held in this workbook in Excel format
        workbook.write(); 
        //Close and free allocated memory 
        workbook.close(); 
    }
}

JXL APIwiki allows users to read, write, create, and modify sheets in an Excel(.xls) workbook at runtime. It doesn't support .xlsx format.

  • JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls
  • For .xlsx use Apache POI.


来源:https://stackoverflow.com/questions/17979749/java-lang-nullpointerexception-when-appending-information-to-an-existing-excel-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!