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

EasyExcel2.2.2不创建对象的写出现了NullPointExcepton #1765

Closed
liubenwei opened this issue Jan 18, 2021 · 2 comments · Fixed by #1885
Closed

EasyExcel2.2.2不创建对象的写出现了NullPointExcepton #1765

liubenwei opened this issue Jan 18, 2021 · 2 comments · Fixed by #1885
Labels
development completed Development completed, waiting for release pending verification This problem needs to be confirmed suggest Suggest

Comments

@liubenwei
Copy link

liubenwei commented Jan 18, 2021

环境:win10,JDK1.8
版本:2.2.2
我在使用不创建对象写的时候,把数据都放在List<List>中,但是有的时候某些属性是null,然后整个list就是[["aaa",null,"bbb"]]这种形式。然后我发现只要List里面有值为null,就会报NullPointExcepton。
调用链:
抛出异常的地方:com.alibaba.excel.converters.ConverterKeyBuild.buildKey()

public static String buildKey(Class clazz) {
String className = clazz.getName();//clazz为null,抛出空指针异常
String boxingClassName = BOXING_MAP.get(clazz.getName());
if (boxingClassName == null) {
return className;
}
return boxingClassName;
}

然后我在顺着调用链发现在ExcelWriteAddExecutor的第165行有对传入的值校验,代码如下:

CellData cellData = converterAndSet(currentWriteHolder, value == null ? null : value.getClass(), cell,
value, null, null, relativeRowIndex);

value就是要写入excel的值。这里有对value的值判空,但如果是null 的话还是把null传下去了,导致在buildKey()的时候出现了NullPointExcepton。
异常的最后的调用链如下:

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.ExcelWriteAddExecutor.doAddBasicTypeToExcel(ExcelWriteAddExecutor.java:109)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addBasicTypeToExcel(ExcelWriteAddExecutor.java:85)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:65)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:52)
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:58)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146)

期待能够在后续版本中优化一下。

我目前的解决是先判断传入的值是不是为null,为null的话 给了一个默认值。

@liubenwei liubenwei added the suggest Suggest label Jan 18, 2021
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Apr 9, 2021
@SmireG SmireG mentioned this issue Apr 18, 2021
2 tasks
@SmireG
Copy link
Contributor

SmireG commented Apr 25, 2021

我尝试解决了这个问题,问题原因出自equal不可以null.equal(str),这样会给出错误 @zhuangjiaju

@zhuangjiaju zhuangjiaju added the development completed Development completed, waiting for release label Sep 15, 2021
@zhuangjiaju
Copy link
Collaborator

已经在3.0.0-beta1 版本修复,beta版本会在一个月内升级成正式版。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development completed Development completed, waiting for release pending verification This problem needs to be confirmed suggest Suggest
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants