Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jun 16, 2022 1:10 pm

I've run into an error that I can't understand or resolve. In a word document I have the string "<start>pepe<end>", nothing more. I use findPattern to locate "<start>" and "<end>". This does it fine but then I have to grab the ranges and use "getAsOneRange" for each of them. With "<start>" it works, but with "<end>" a null pointer error is thrown. I have also tried with the "getAsRange" and "getRanges" methods and I get the same error.
The most curious thing is that I have been testing Spire.Doc for several days and I have other documents with much more content with many marks to search for and they work well. I ran into this error when in the large document, I put the marks inside a table cell. When trying to reproduce the error in a simpler document, I arrived at the document that I am attaching, which can not be simpler.

The code:

Document document = new Document();
document.loadFromFile("PruebaWord con tabla.docx");
TextSelection startSelection = document.findPattern(Pattern.compile("<start>"));
TextSelection endSelection = document.findPattern(Pattern.compile("<end>"));
TextRange startRange = startSelection.getAsOneRange();
TextRange endRange = endSelection.getAsOneRange();

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Fri Jun 17, 2022 5:39 am

Hi,

Because the getAsOneRange method will change the structure of the current paragraph. This can cause errors when the start and end marks are in the same paragraph. Please refer to the code below to adjust the order of method calls to avoid errors.
Code: Select all
        TextSelection startSelection = document.findPattern(Pattern.compile("<start>"));
        TextRange startRange = startSelection.getAsOneRange();
        TextSelection endSelection = document.findPattern(Pattern.compile("<end>"));
        TextRange endRange = endSelection.getAsOneRange();
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Fri Jun 17, 2022 7:43 am

works fine, thanks

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Return to Spire.Doc