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

当数据列表里面有值为空的时候使用会触发空指针异常 #1274

Closed
gao1399677 opened this issue Apr 27, 2020 · 1 comment
Closed
Assignees
Labels
bug Something isn't working pending verification This problem needs to be confirmed

Comments

@gao1399677
Copy link

gao1399677 commented Apr 27, 2020

触发场景描述
当数据列表里面有值为空的时候使用会触发空指针异常
版本2.2.2

触发Bug的代码
com.alibaba.easyexcel.test.demo.fill.FillTest#data中的// fillData.setName("张三");注释掉
单元测试com.alibaba.easyexcel.test.demo.fill.FillTest#compositeFill会触发bug,下面是我使用时触发的代码

BiConsumer<ExcelWriter, WriteSheet> doFill = (writer, sheet) -> {
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            writer.fill(new FillWrapper("a", register.getUnRegisterList()), sheet);
            writer.finish();
        };

提示的异常或者没有达到的效果

java.lang.NullPointerException
	at com.alibaba.excel.converters.ConverterKeyBuild.buildKey(ConverterKeyBuild.java:29)
	at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.doConvert(AbstractExcelWriteExecutor.java:103)
	at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.convert(AbstractExcelWriteExecutor.java:93)
	at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.converterAndSet(AbstractExcelWriteExecutor.java:41)
	at com.alibaba.excel.write.executor.ExcelWriteFillExecutor.doFill(ExcelWriteFillExecutor.java:202)
	at com.alibaba.excel.write.executor.ExcelWriteFillExecutor.fill(ExcelWriteFillExecutor.java:118)
	at com.alibaba.excel.write.ExcelBuilderImpl.fill(ExcelBuilderImpl.java:78)
	at com.alibaba.excel.ExcelWriter.fill(ExcelWriter.java:185)
	at com.alibaba.easyexcel.test.demo.fill.FillTest.compositeFill(FillTest.java:200)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

将com.alibaba.excel.write.executor.AbstractExcelWriteExecutor#doConvert的

if (value == null && !(converter instanceof NullableObjectConverter)) {
            return new CellData(CellDataTypeEnum.EMPTY);
    }提到最前放面做判断不会触发错误
private CellData doConvert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
        ExcelContentProperty excelContentProperty) {
        Converter converter = null;
        if (excelContentProperty != null) {
            converter = excelContentProperty.getConverter();
        }
        if (value == null && !(converter instanceof NullableObjectConverter)) {
            return new CellData(CellDataTypeEnum.EMPTY);
        }
        if (converter == null) {
            converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
        }
        if (converter == null) {
            throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
                new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
                "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
        }
        CellData cellData;
        try {
            cellData =
                converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration());
        } catch (Exception e) {
            throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
                new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
                "Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(), e);
        }
        if (cellData == null || cellData.getType() == null) {
            throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
                new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
                "Convert data:" + value + " return null,at row:" + cell.getRow().getRowNum());
        }
        return cellData;
    }
@gao1399677 gao1399677 added the bug Something isn't working label Apr 27, 2020
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Apr 27, 2020
@zhuangjiaju
Copy link
Collaborator

不好意思。新改出来了的bug,麻烦升级到2.2.3

dota17 pushed a commit to dota17/easyexcel that referenced this issue May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending verification This problem needs to be confirmed
Projects
None yet
Development

No branches or pull requests

2 participants