0

Good day people. I am working on a java-ee project, specifically working on exporting a datable to spreadhseet(xls). I have a method called postProcessXLS. I use this method to auto adjust the column width of the generated spreadsheet and also to add cell styles and formatting. When i run the app, the method postProcessXLS throws a null pointer exception without referencing the line.

bean

 public void postProcessXLS(Object document) throws IOException,     BadElementException, DocumentException {
    try {
        HSSFWorkbook wb = (HSSFWorkbook)document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header = sheet.getRow(0);

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = header.getCell(i);

            cell.setCellStyle(cellStyle);
        }

        for (int colNum = 0; colNum < header.getLastCellNum(); colNum++) {
            sheet.autoSizeColumn(colNum);
        }
    } catch (Exception ex) {
        LOG.error("Could not process spreadsheet" + ex);
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
akin
  • 1
  • 6
  • Post you exception as logged by `LOG.error("Could not process spreadsheet" + ex);` – Scary Wombat Feb 16 '16 at 05:22
  • `Could not process spreadsheetjava.lang.NullPointerException` – akin Feb 16 '16 at 05:24
  • I suggest that the document you are passing in maybe not be a `HSSFWorkbook` - why does the method take an `Object` tather than a `HSSFWorkbook` ? – Scary Wombat Feb 16 '16 at 05:26
  • I found the method in primefaces showcase. so i just decided to modify it. I added the auto column size line so it auto sizes the columns of the generated spreadsheet. Can you please improve the method? thanks – akin Feb 16 '16 at 05:31
  • Sure, my going rate is $100 per hour. Please let me know how you want to pay. – Scary Wombat Feb 16 '16 at 05:35
  • Once you get exception handling right and you're capable to ask "Why is variable X null?" instead of "Why do I get a NullPointerException?" feel free to ask a new and clear cut question with a real MCVE. – BalusC Feb 16 '16 at 08:21

0 Answers0