Apache POI解析word为html出现:......converter.core.XWPFConverterException: java.lang.NullPointerException

使用Apache poi解析word文档为html文件时出现空指针异常:

报错一:

org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:77)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:63)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:61)

报错二:

Caused by: java.lang.NullPointerException: null
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.getXWPFNum(XWPFDocumentVisitor.java:473)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.getNumPr(XWPFDocumentVisitor.java:340)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraph(XWPFDocumentVisitor.java:285)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:247)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableCellBody(XWPFDocumentVisitor.java:1107)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitCell(XWPFDocumentVisitor.java:1042)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableRow(XWPFDocumentVisitor.java:946)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableBody(XWPFDocumentVisitor.java:887)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:869)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:251)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:73)

格式化显示为:

org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
	at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:77)
	at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:63)
	at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
	at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:61)
..................
Caused by: java.lang.NullPointerException: null
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.getXWPFNum(XWPFDocumentVisitor.java:473)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.getNumPr(XWPFDocumentVisitor.java:340)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraph(XWPFDocumentVisitor.java:285)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:247)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableCellBody(XWPFDocumentVisitor.java:1107)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitCell(XWPFDocumentVisitor.java:1042)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableRow(XWPFDocumentVisitor.java:946)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTableBody(XWPFDocumentVisitor.java:887)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:869)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:251)
	at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199)
	at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:73)

解决:看到空指针异常,很多人首先会认为是自己的代码写错了。事实上,这是Apache组件写的很粗糙造成的,该判空的地方没有判空。这里有两个地方出现了控制异常。出现问题的地方估计就是两个了。

第一个空指针出现在RunUnderlineValueProvider类(package org.apache.poi.xwpf.converter.core.styles.run;)。看到这个U,我猜想是解析下划线或者是表格出现了错误。
在这里插入图片描述
此处在进行rPr.getU().getVal()操作时没有进行判空操作,出现了空指针异常。

解决:在自己的工程下创建一个一模一样的包,然后创建一个一模一样的类,并更改为下面代码:

package org.apache.poi.xwpf.converter.core.styles.run;

import java.util.Objects;

import org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline.Enum;

public class RunUnderlineValueProvider extends AbstractRunValueProvider<UnderlinePatterns> {

    /** INSTANCE */
    public static final RunUnderlineValueProvider INSTANCE = new RunUnderlineValueProvider();

    @Override
    public UnderlinePatterns getValue(CTRPr rPr, XWPFStylesDocument stylesDocument) {

        if (Objects.isNull(rPr)) {
            return null;
        }

        if (rPr.isSetU()) {

            CTUnderline ctu = rPr.getU();

            if (Objects.nonNull(ctu)) {

                Enum val = ctu.getVal();

                if (Objects.nonNull(val)) {
                    return UnderlinePatterns.valueOf(val.intValue());
                }
            }
        }
        return null;
    }
}

第二个空指针出现在XWPFDocumentVisitor类(package org.apache.poi.xwpf.converter.core;),下面的红框内即是出现空指针的地方。
在这里插入图片描述
解决:在自己的工程下创建一个一模一样的包,然后创建一个一模一样的类,并修改图示代码为:

    protected XWPFNum getXWPFNum(CTNumPr numPr) {
    
        CTDecimalNumber numID = numPr.getNumId();
        
        if (Objects.isNull(numID)) {
            return null;
        }

        XWPFNumbering numbering = document.getNumbering();

        if (Objects.isNull(numbering)) {
            return null;
        }

        XWPFNum num = numbering.getNum(numID.getVal());
        
        return num;
    }

两处包名为:
在这里插入图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值