easyexcel 使用fill(Object....)接口出现的空指针问题

xqkwcwgp  于 4个月前  发布在  其他
关注(0)|答案(2)|浏览(45)

触发场景描述

直接产生异常处:

对应的调用报错栈
java.lang.NullPointerException
at com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy.afterRowDispose(AbstractRowHeightStyleStrategy.java:22)
at com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain.afterRowDispose(RowHandlerExecutionChain.java:47)
at com.alibaba.excel.util.WriteHandlerUtils.afterRowDispose(WriteHandlerUtils.java:178)
at com.alibaba.excel.write.executor.ExcelWriteFillExecutor.doFill(ExcelWriteFillExecutor.java:297)
at com.alibaba.excel.write.executor.ExcelWriteFillExecutor.fill(ExcelWriteFillExecutor.java:130)
at com.alibaba.excel.write.ExcelBuilderImpl.fill(ExcelBuilderImpl.java:82)
at com.alibaba.excel.ExcelWriter.fill(ExcelWriter.java:110)
at com.alibaba.excel.ExcelWriter.fill(ExcelWriter.java:98)

图示:

触发Bug的代码

@Test
    public void testDemo() {

        WriteSheet writeSheet = EasyExcelFactory.writerSheet("Sheet1").build();
        ExcelWriter excelWriter = EasyExcel.write(fileName)
                // 加入拦截就会出现问题
                .registerWriteHandler(new CustomCellWriteHeightConfig())
                .withTemplate(testTemplate)
                .build();

        // 这里 会填充到第一个sheet, 然后文件流会自动关闭
        FillData fillData = new FillData();
        fillData.setName("张三");
        fillData.setNumber(5.2);

        excelWriter.fill(fillData,writeSheet);
        excelWriter.finish();
    }

个人想法

doFill(readTemplateData(templateAnalysisCache), realData, fillConfig, null);

RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext,
null, relativeRowIndex, Boolean.FALSE);

并且其中的 relativeRowIndexhead 属性都是上下文固定传入的,在 AbstractRowHeightStyleStrategy 类中的方法
setHeadColumnHeightsetContentColumnHeight

if (context.getHead()) {
setHeadColumnHeight(context.getRow(), context.getRelativeRowIndex());
} else {
setContentColumnHeight(context.getRow(), context.getRelativeRowIndex());
}

用的是基类int接收参数null是不允许的。

考虑是否能换成包装类型接收,若有不能空的逻辑则再做判空处理。

fzsnzjdm

fzsnzjdm1#

这个bug到现在没修复吗?遇到同样问题了

mtb9vblg

mtb9vblg2#

同样的问题,使用doFill()出现,更新至 4.0.2仍然存在,这是我的报错:

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "com.alibaba.excel.write.handler.context.RowWriteHandlerContext.getRelativeRowIndex()" is null
	at com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy.afterRowDispose(AbstractRowHeightStyleStrategy.java:22)

相关问题