建议先去看文档
快速开始 、 常见问题
触发场景描述
当class head 和list head大小不一致时,ExcelWriteHeadProperty构造函数会报空指针错误
触发Bug的代码
1. public ExcelWriteHeadProperty(Holder holder, Class<?> headClazz, List<List<String>> head) {
2. super(holder, headClazz, head);
3. if (getHeadKind() != HeadKindEnum.CLASS) {
4. return;
5. }
6. this.headRowHeightProperty =
7. RowHeightProperty.build(headClazz.getAnnotation(HeadRowHeight.class));
8. this.contentRowHeightProperty =
9. RowHeightProperty.build(headClazz.getAnnotation(ContentRowHeight.class));
10. this.onceAbsoluteMergeProperty =
11. OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class));
12.
13. ColumnWidth parentColumnWidth = headClazz.getAnnotation(ColumnWidth.class);
14. HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class);
15. HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class);
16.
17.
18. for (Map.Entry<Integer, Head> entry : getHeadMap().entrySet()) {
19. Head headData = entry.getValue();
20. if (headData == null) {
21. throw new IllegalArgumentException(
22. "Passing in the class and list the head, the two must be the same size.");
23. }
24. Field field = headData.getField();
25. if (field != null) {
26. ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class);
27. if (columnWidth == null) {
28. columnWidth = parentColumnWidth;
29. }
30. headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth));
31.
32.
33. HeadStyle headStyle = field.getAnnotation(HeadStyle.class);
34. if (headStyle == null) {
35. headStyle = parentHeadStyle;
36. }
37. headData.setHeadStyleProperty(StyleProperty.build(headStyle));
38.
39. HeadFontStyle headFontStyle = field.getAnnotation(HeadFontStyle.class);
40. if (headFontStyle == null) {
41. headFontStyle = parentHeadFontStyle;
42. }
43. headData.setHeadFontProperty(FontProperty.build(headFontStyle));
44.
45. headData.setLoopMergeProperty(LoopMergeProperty.build(field.getAnnotation(ContentLoopMerge.class)));
46. } else {
47. headData.setColumnWidthProperty(ColumnWidthProperty.build(null));
48. headData.setHeadStyleProperty(StyleProperty.build((HeadStyle) null));
49. headData.setHeadFontProperty(FontProperty.build((ContentFontStyle) null));
50. headData.setLoopMergeProperty(LoopMergeProperty.build(null));
51. }
52. }
53. }
提示的异常或者没有达到的效果
当class head 和list head大小不一致时,实际第20行headData并不为null(有headNameList但是没有field),不会触发IllegalArgumentException,会在第24行触发NPE,这里不明白为什么需要限制两者size一致。
我有个需求是希望头有4列,但是写入的数据只需要写前3列,而且希望按照实体类的方式写出(参考variableTitleWrite()的方式),上面代码加了一个if else判断可以成功写出,不知道两者不一致会有什么隐患。
1条答案
按热度按时间vfwfrxfs1#
希望实现的效果见附件:
variableTitleWrite1661337881256.xlsx