question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullException while creating document -- TextBlock.GetBody

See original GitHub issue

Thank you very much for this project.

I have problem using it though, because right at the start, when you are loading/creating the document with DocumentFactory.Create I got null exception. The issue is with TextBlock.GetBody and its loop while (nextElement != endParent).

Inside the body there is soft casting to OpenXmlCompositeElement on the assumption that every element is indeed OpenXmlCompositeElement, but if it is not the case, the outcome of the cast would be null, and on next iteration null exception will be thrown.

And in my case it is what happens, at one point DocumentFormat.OpenXml.Wordprocessing.BookmarkEnd appears (it does not inherit from OpenXmlCompositeElement).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
egonlcommented, Jan 21, 2022

Could you also post how you’re calling SharpDocx? I guess you’re doing something like:

            var document = DocumentFactory.Create(viewPath);

instead of

            var document = DocumentFactory.Create(viewPath, model);

DocumentFactory needs to know the type of the model. If it doesn’t, it will generate public string Model { get; set; } instead of public MyViewModelType Model { get; set; }. Which will lead to a compilation error in foreach (var sensor in Model.Sensors).

0reactions
astrowalkercommented, Jan 21, 2022

Many thanks for the update. Indeed I had such combo

Create...
SetModel

and the first one failed. Now I have Create with two arguments as you shown, and the creation was successful. So I think it is safe to close this report as fixed. Once again, thank you very much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

XWPFWordExtractor.getText() throws NullPointerException
However, that itself throws a NullPointerException because it tries to access the "SectPr" of the document via doc.getDocument().getBody().
Read more >
RetrofitError: getBody can throw a NullPointerException #405
It seems they're in between implementations for JUnit in Android Studio - creating tests isn't a right click away anymore, or maybe I...
Read more >
Handling Java NullPointerException and Best Practices
NullPointerException is a runtime condition where we try to access or modify an object which has not been initialized yet. It essentially means ......
Read more >
How to fix Attempt to de-reference a null object in test for ...
This is pretty straightforward. Your code is designed to return a null value given an invalid response, and that's exactly what it's doing....
Read more >
Null Pointer Exception In Java
NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found