Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException thrown when templating #266

Open
Blackiris opened this issue Sep 7, 2023 · 0 comments
Open

NullPointerException thrown when templating #266

Blackiris opened this issue Sep 7, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@Blackiris
Copy link

Description

A nullpointerexception is thrown when templating.

java.lang.NullPointerException
	at org.apache.poi.hssf.record.CFHeaderBase.serialize(CFHeaderBase.java:144)
	at org.apache.poi.hssf.record.StandardRecord.serialize(StandardRecord.java:56)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook$SheetRecordCollector.serialize(HSSFWorkbook.java:1476)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1523)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1418)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1404)
	at org.jxls.transform.poi.PoiTransformer.writeButNotCloseStream(PoiTransformer.java:501)
	at org.jxls.transform.poi.PoiTransformer.write(PoiTransformer.java:482)
	at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:288)
	at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:253)

Reproduction code and file

    @Test
    public void processTemplate_with_good_data_expected_outputfile() throws FileNotFoundException {
        FileTemplaterExcel fileTemplaterExcel = new FileTemplaterExcel();

        Map<String, Object> data = new HashMap<>();
        DistributeSlipProcessed dist = new DistributeSlipProcessed();
        dist.setCategory("ABC");

        JtpDistributeDocument doc = new JtpDistributeDocument();
        doc.setDocumentTitle("Mon titre");
        doc.setDocumentReference("REF04846");
        doc.setDocumentVersion("1.0");
        doc.setJtpLodToClient("OUI");

        dist.setDocuments(List.of(doc, doc));

        data.put("distributes", List.of(dist));
        FileOutputStream fout = new FileOutputStream("file1.xls");
        fileTemplaterExcel.processTemplate("template3.xls", data, fout);
    }

template3.xls

Solution found

It seems that when a conditionalFormatting is not used, instead of setting an empty list of ranges, it is better to delete it.

Might not be the solution, but changing removeConditionalFormatting in PoiTransformer.java makes it works:

    // this method updates conditional formatting ranges only when the range is inside the passed areaRef
    private void removeConditionalFormatting(AreaRef areaRef) {
        Sheet destSheet = workbook.getSheet(areaRef.getSheetName());
        CellRangeAddress areaRange = CellRangeAddress.valueOf(areaRef.toString());
        SheetConditionalFormatting sheetConditionalFormatting = destSheet.getSheetConditionalFormatting();
        int numConditionalFormattings = sheetConditionalFormatting.getNumConditionalFormattings();

        for (int index = numConditionalFormattings -1; index >= 0; index--) {
            ConditionalFormatting conditionalFormatting = sheetConditionalFormatting.getConditionalFormattingAt(index);
            CellRangeAddress[] ranges = conditionalFormatting.getFormattingRanges();
            List<CellRangeAddress> newRanges = new ArrayList<>();
            for (CellRangeAddress range : ranges) {
                if (!areaRange.isInRange(range.getFirstRow(), range.getFirstColumn()) || !areaRange.isInRange(range.getLastRow(), range.getLastColumn())) {
                    newRanges.add(range);
                }
            }
            if (newRanges.isEmpty()) {
                sheetConditionalFormatting.removeConditionalFormatting(index);
            } else {
                conditionalFormatting.setFormattingRanges(newRanges.toArray(new CellRangeAddress[] {}));
            }
        }
    }

@Blackiris Blackiris added the bug Something isn't working label Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants