Insertfield with PAGE or NUMPAGES causes a NullReferenceException when calling UpdateFields

Hello,

I’ve just discovered a quite strange behavior. Don’t know if it is a bug, but I definitely had troubles getting to the root cause:

If you insert a PAGE or NUMPAGES word-field without a second argument to InsertField(…) into a document that already has a SEQ field - a following call to UpdateFields() will cause a NullReferenceException.

If you insert a PAGE or NUMPAGES word-field WITH a second argument to InsertField(…) it works regardless if there is a SEQ field already in the document.

So, the documentation should state that inserting PAGE or NUMPAGES fields requires a second (possibly empty) argument to InsertField(…) - or it is a bug that need to be fixed.

I have created a test project in VS2010 which I have attached. The test was performed with Aspose.Words 11.1.0.0

Here is the code:

class Program
{
    static void Main(string[] args)
    {
        // testinput_with_seq.docx is a word document with a Seq field inserted like this:
        // SEQ Tabel * ARABIC

        // testinput_without_seq.docx is an empty word document

        License license = new License();
        license.SetLicense("Aspose.Words.lic");

        string testFileWithSeq = "testinput_with_seq.docx";
        string testFileWithoutSeq = "testinput_without_seq.docx";

        // inserting a PAGE word field into a document with a SEQ field
        // already in it with no second argument:
        // builder.InsertField(@"PAGE");
        // will cause UpdateFields() to throw a NullReferenceException
        try
        {
            InsertOneArgument(testFileWithSeq, "output1.docx");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Expected: InsertOneArgument failed for file " + testFileWithSeq);
        }

        // here, there is no SEQ field, so
        // builder.InsertField(@"PAGE");
        // will not fail. It will show the pagenumber, though - so the second argument isn't strictly required
        try
        {
            InsertOneArgument(testFileWithoutSeq, "output2.docx");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Not expected: InsertOneArgument failed for file " + testFileWithoutSeq);
        }

        // now we try with a SEQ field, but also with two arguments (an empty second one)
        // builder.InsertField(@"PAGE", "");
        // and it works
        try
        {
            InsertTwoArguments(testFileWithSeq, "output3.docx");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Not expected: InsertTwoArguments failed for file " + testFileWithSeq);
        }

        // without a sequence number and with two arguments also works
        try
        {
            InsertTwoArguments(testFileWithoutSeq, "output4.docx");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Not expected: InsertTwoArguments failed for file " + testFileWithoutSeq);
        }
    }

    private static void InsertOneArgument(string inputFilename, string outputFilename)
    {
        Document doc = new Document(Path.Combine(Directory.GetCurrentDirectory(), inputFilename));
        DocumentBuilder builder = new DocumentBuilder(doc);

        builder.Write("Page ");
        builder.InsertField(@"PAGE");

        doc.UpdateFields();

        doc.Save(Path.Combine(Directory.GetCurrentDirectory(), outputFilename));
    }

    private static void InsertTwoArguments(string inputFilename, string outputFilename)
    {
        Document doc = new Document(Path.Combine(Directory.GetCurrentDirectory(), inputFilename));
        DocumentBuilder builder = new DocumentBuilder(doc);

        builder.Write("Page ");
        builder.InsertField(@"PAGE", "");

        doc.UpdateFields();

        doc.Save(Path.Combine(Directory.GetCurrentDirectory(), outputFilename));
    }
}

Hi Jan,

Thanks for the additional information. While using latest Aspose.Words 11.1.0. I managed to reproduce this problem on my side. I have logged your issue as WORDSNET-6031 into our bug tracking system. Your request has also been linked to the appropriate issue. Once we sort it out, we will let you know. Sorry for inconvenience.

Hi Jan,

Thanks for your inquiry.

Just as a note, the reason why the second overload with the two parameters works is because that particular overload inserts the field without updating it. That is, the result is inserted with what ever you pass instead of the value being calculated.

The first overload with only parameter will update the field when it is inserted.

Thanks,

Hi,

Thanks for your quick response. I don’t quite understand the “without updating it”-part, though :o)

When I insert the PAGE field in an empty document, it just ends up as “1” regardless of which overload I call - so in my view there is nothing wrong (isolated) with the insertfield() method combined with the PAGE / NUMPAGES fields.

When I insert the PAGE field in a document with a SEQ field, it followingly crashes when I call updatefields. Not when I insert it. So thats why I’m a bit confused about your explanation.

Ah, well… I’m sure you understand the technicalities in MS Word-world a lot better than me - I’m just looking forward to a bugfix :o)

Thanks for looking at it late at night…

/Jan

Hi Jan,

Thanks for this additional information.

You’re correct, I misinterpreted where exactly the exception was occurring, my apologies. In any case this is definitely a bug and we will look into fixing it as soon as possible.

Thanks,

The issues you have found earlier (filed as WORDSNET-6031) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.