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

java.lang.NullPointerException when converting DocX to html #155

Open
ZidZabre opened this issue Aug 22, 2016 · 16 comments
Open

java.lang.NullPointerException when converting DocX to html #155

ZidZabre opened this issue Aug 22, 2016 · 16 comments

Comments

@ZidZabre
Copy link

Stack
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:45)
at [Redacted].DocImport.StringDOCXImport(DocImport.java:270)

Olika program att använda sig utav med GTD för att få det enklare i vardagen.docx

including docx file.

Any ideas on whats causing this?

@pensionarchitects-ben
Copy link
Contributor

If you send the whole Stacktrace this could be found

@ZidZabre
Copy link
Author

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:45)
at [Redacted].DocImport.StringDOCXImport(DocImport.java:270)
at [Redacted].GuideLogic.DocxToTextGuide(GuideLogic.java:725)
at [Redacted].GuideRest.convert(GuideRest.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:534)
at org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:482)
at org.glassfish.jersey.servlet.ServletContainer.doFilter(ServletContainer.java:419)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:89)
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119)
at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133)
at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130)
at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203)
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at [Redacted].doFilter([Redacted].java:83)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

@pensionarchitects-ben
Copy link
Contributor

I need to know where the null pointer happens, this stacktrace is still incomplete

@ZidZabre
Copy link
Author

that is the trace i get out of it.

@pensionarchitects-ben
Copy link
Contributor

Can you debug it and check where the null pointer happens?

@ZidZabre
Copy link
Author

my appalogies seems i forgot to add this:

Caused by: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil.getWidth(XWPFTableUtil.java:273)
at org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:284)
at org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:127)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:867)
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)
... 60 more

Also the document enclosed causes this error, as other documents doesn't.
also code snippet:

        XWPFDocument document = new XWPFDocument(in);
        XHTMLOptions options = XHTMLOptions.create();
        XHTMLConverter.getInstance().convert(document, out, options);

input and output streams is defined. and as stated before, other docx documents works without issues, but some others causes this.

@pensionarchitects-ben
Copy link
Contributor

Looks like something weird, there doesn't seem to be a table in the document. Still it tries to parse one. Btw which version of XDocReport are you using? Can you remake your document part for part and fix this problem. By doing that you can find out what part of the document causes the error. I do not have expertise with the DOCX to XWPF converter, maybe just wait for @angelozerr to come by.

@shuanshuan
Copy link

Is this fixed, I have the same problem

@max-young
Copy link

I have the same problem, here is my code:

XHTMLConverter.getInstance().convert(docxDocument, htmlStream, options)

error information:

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:45)
	at com.ksxing.common.utils.OfficeToolService.uploadDocxToHtml(OfficeToolService.java:71)

@x0zh
Copy link

x0zh commented Nov 7, 2019

I have the same problem, here is the stacktrace:

fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException

	at fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:78)
	at fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:64)
	at fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:39)
	at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:62)
// ...
Caused by: java.lang.NullPointerException
	at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.getGridColList(XWPFTableUtil.java:173)
	at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:106)
	at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:970)
	at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:267)
	at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:215)
	at fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:74)
	... 27 more

I debug it and find the null pointer happens in fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil#computeColWidths()

image

@mp-loki
Copy link

mp-loki commented Jan 24, 2020

Having the same problem. grid is null for some reason and no clue how it should be initialized

@badmalloc
Copy link

Any updates for this bug?

@lwlach
Copy link

lwlach commented Oct 22, 2020

I have the same problem. I notice it only happens when the document is generated um apache poi. If eu edit in any way the docx file in word and try to convert after, it works. Somehow the API is not filling all table fileds and causa these null pointers

@ezelorenzatti
Copy link

ezelorenzatti commented Dec 15, 2020

I have the same problem, the document has a table created with apache poi 4.0.1 and CTTblGrid is null

java.lang.NullPointerException: null at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.getGridColList(XWPFTableUtil.java:184) ~[fr.opensagres.poi.xwpf.converter.core-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.core.utils.XWPFTableUtil.computeColWidths(XWPFTableUtil.java:117) ~[fr.opensagres.poi.xwpf.converter.core-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitTable(XWPFDocumentVisitor.java:970) ~[fr.opensagres.poi.xwpf.converter.core-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:267) ~[fr.opensagres.poi.xwpf.converter.core-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:215) ~[fr.opensagres.poi.xwpf.converter.core-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57) ~[fr.opensagres.poi.xwpf.converter.pdf-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39) ~[fr.opensagres.poi.xwpf.converter.pdf-2.0.2.jar:2.0.2] at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46) ~[fr.opensagres.poi.xwpf.converter.core-2.0.2.jar:2.0.2]

image

@Xiaoshu-Zhao
Copy link

I got the same issue and I solved it. The following are the steps I did and hope it is helpful.

First of all, XWPFConverterException is confused and not helpful to debug because it is throwed by many different errors. Here is the source code:
image
As we can see, any error occur during convert process may throw XWPFConverterException.

However, I used the debug mode and found the error occur in ListItemContext class which package path is fr.opensagres.poi.xwpf.converter.core. The method getText are following:

    public String getText() {
        String text = this.lvl.getLvlText().getVal();
        CTNumFmt numFmt = this.lvl.getNumFmt();
        if (!STNumberFormat.BULLET.equals(numFmt)) {
            List<String> numbers = new ArrayList();

            for (ListItemContext item = this; !item.isRoot(); item = item.getParent()) {
                numbers.add(0, item.getNumberText());
            }

            String num = null;

            for (int i = 0; i < numbers.size(); ++i) {
                if (numbers.get(i) instanceof String) {
                    num = (String) numbers.get(i);
                }
                text = StringUtils.replaceAll(text, "%" + (i + 1), num);
            }
        }

        return text;
    }

And I found String text = this.lvl.getLvlText().getVal(); is the root cause. this.lvl.getLvlText() can be null and then it will throw NullPointerException. So I just add the following code at the begin of the method.

if (this.lvl == null || this.lvl.getLvlText() == null) {
            return "";
        }

Then I can convert Docx to html without any problem.

@Xiaoshu-Zhao
Copy link

I got the same issue and I solved it. The following are the steps I did and hope it is helpful.

First of all, XWPFConverterException is confused and not helpful to debug because it is throwed by many different errors. Here is the source code: image As we can see, any error occur during convert process may throw XWPFConverterException.

However, I used the debug mode and found the error occur in ListItemContext class which package path is fr.opensagres.poi.xwpf.converter.core. The method getText are following:

    public String getText() {
        String text = this.lvl.getLvlText().getVal();
        CTNumFmt numFmt = this.lvl.getNumFmt();
        if (!STNumberFormat.BULLET.equals(numFmt)) {
            List<String> numbers = new ArrayList();

            for (ListItemContext item = this; !item.isRoot(); item = item.getParent()) {
                numbers.add(0, item.getNumberText());
            }

            String num = null;

            for (int i = 0; i < numbers.size(); ++i) {
                if (numbers.get(i) instanceof String) {
                    num = (String) numbers.get(i);
                }
                text = StringUtils.replaceAll(text, "%" + (i + 1), num);
            }
        }

        return text;
    }

And I found String text = this.lvl.getLvlText().getVal(); is the root cause. this.lvl.getLvlText() can be null and then it will throw NullPointerException. So I just add the following code at the begin of the method.

if (this.lvl == null || this.lvl.getLvlText() == null) {
            return "";
        }

Then I can convert Docx to html without any problem.

this problem should be solved. Check this link for detail: #638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants