We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Inserting document leads to null reference exception

I have gotten null reference exception from Aspose while inserting one document (source) into another (destination).
First of all I am removing the only one StructuredDocumentTag from source document. After that I am inserting it into destination document. For insertion I am using DocumentBuilder - InsertDocument.

Exception :

System.NullReferenceException was caught
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Aspose.Words
StackTrace:
at . (Document , ImportFormatMode , )
at . (Document , ImportFormatMode , )
at Aspose.Words.DocumentBuilder.InsertDocument(Document srcDoc, ImportFormatMode importFormatMode)
at Test.GenDoc.Foresters_Flexible_Purchase2() in d:\Programming\Test\Test\GenDoc.cs:line 810
at Test.Program.Main(String[] args) in d:\Programming\Test\Test\Program.cs:line 30
InnerException:

What I found as a clue is that in the source document the only one and also last one node is SDT that I am removing. Otherwise if I add one new row (translated to new paragraph) than I will not get this exception. Also if I remove SDT, than save document and than insert it into destination document I will not be faced with this exception. Looks like structure of the source document after SDT removal is problematic to add to destination document like this.
Also as an workaround is possible that before SDT removal I add at the document end, empty paragraph if next sibling is null.

Whatever, I find this is not expected behavior and looks like a bug. Could you please investigate ?

Code snippet :

public static void NullReferenceIssue()
{
Document destDoc = new Document();
Document sourceDoc = new Document("OneSDTDoc.docx");

StructuredDocumentTag sdt =
sourceDoc.FirstSection.Body.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast ().FirstOrDefault();

sdt.Remove();
DocumentBuilder builder = new DocumentBuilder(destDoc);
builder.MoveToDocumentEnd();
builder.InsertDocument(sourceDoc, ImportFormatMode.KeepSourceFormatting);

string documentName = Guid.NewGuid() + ".docx";
destDoc.Save(documentName, SaveFormat.Docx);
}



Hi Rastko,

Thanks for your inquiry. Please call Document.EnsureMinimum method after removing the content control (SDT). If the document contains no sections, this method creates one section with one paragraph.

The Document can contain only Section objects. A minimal valid section needs to have Body with one Paragraph. We suggest you please read following article:
Aspose.Words Document Object Model

Document destDoc = new Document();

Document sourceDoc = new Document(MyDir + "OneSDTDoc.docx");

StructuredDocumentTag sdt =

sourceDoc.FirstSection.Body.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().FirstOrDefault();

sdt.Remove();

sourceDoc.EnsureMinimum();

DocumentBuilder builder = new DocumentBuilder(destDoc);

builder.MoveToDocumentEnd();

builder.InsertDocument(sourceDoc, ImportFormatMode.KeepSourceFormatting);

destDoc.Save(MyDir + "Out.docx", SaveFormat.Docx);

Thank you Tahir, this works perfectly !

Hi Rastko,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.