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

Free Support Forum - aspose.com

Third time adding cloned section: "Object reference not set to an instance of an object"

Hello -
This code:

Aspose.Words.License license = new Aspose.Words.License();
Aspose.Words.Document doc = new Aspose.Words.Document(@"s:\Replacement tokens.doc");
// Get a copy of the first section in the doc so it can be appended to the doc.
Aspose.Words.Section sectionTemplateSource = doc.Sections[0].Clone();
for (int i = 0; i < 5; ++i)
    // If we're not at the first section, append the first section to the doc.
    if (i > 0)

…fails when line 28, the one containing doc.Sections.Add, is executed the third time. The error I receive is:

Object reference not set to an instance of an object.
at Aspose.Words.CompositeNode.RemoveChild(Node oldChild)
at Aspose.Words.CompositeNode.࠮(Node ࠘, Node ࠯, Boolean ࠰)
at Aspose.Words.CompositeNode.InsertAfter(Node newChild, Node refChild)
at Aspose.Words.CompositeNode.AppendChild(Node newChild)
at Aspose.Words.NodeCollection.Add(Node node)
at webforms_AsposeTest.Page_Load(Object sender, EventArgs e) in c:\My Projects\BSI Working\EasyOperator\OperatorMarketing\webforms\AsposeTest.aspx.cs:line 28
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I’ve attached the document referenced in the code.
Do I need to do something different? Please advise.

Thank you for your inquiry.
Document.Sections.Add doesn’t clone source section itself. It just adds what you specify. Please ensure you have one more distinct object before every addition.