Problem with header footer code

Hi I keep getting this error when I debug my application,





An unhandled
exception of type ‘System.NullReferenceException’ occurred in Audit
Exemption.exe



Additional
information: Object reference not set to an instance of an object.



The code for the document and its headers are here,



// Create new Audit Document from Auditdocs template file

Document audDoc = new Document(myDocs + “\” + “Auditdocs.dot”);

audDoc.Document.Sections.Clear();



Section sect = audDoc.Sections[0];



HeaderFooter
header = new HeaderFooter(audDoc, HeaderFooterType.HeaderPrimary);

sect.HeadersFooters.Add(header);



HeaderFooter
footer = new HeaderFooter(audDoc, HeaderFooterType.FooterPrimary);

sect.HeadersFooters.Add(footer);



// Header Text

Paragraph paraHead = new Paragraph(audDoc);

paraHead.AppendChild(new Run(audDoc, "Client Name: " + myName +
" Accounting Period: "

  • myPeriod));

    header.AppendChild(paraHead);



    // Footer Text

    Paragraph paraFoot = new Paragraph(audDoc);

    paraFoot.AppendChild(new Run(audDoc, "Created by: " + myInitials));

    footer.AppendChild(paraFoot);





    The error refers to the line,



    sect.HeadersFooters.Add(header);



    I do not see why this is erroring, I have created an instance of sect
    and an instance of header so why am I being told this is not correct?

After the execution of the following lines:

audDoc.Document.Sections.Clear();
Section sect = audDoc.Sections[0];

sect value is null, because you have explicitly deleted all sections from the document with the Clear command.

Thankyou this works fine, just have a problem which is discussed in my other thread.