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

Free Support Forum - aspose.com

Request: Please stop throwing NullRefenceException when trying to write NULL strings

Hi,

I have a simple “feature” request. I often find myself testing for NULL values before doing a builder.Writeln() because otherwise Aspose will throw an ArgumentNullException : Value cannot be null.error.

This test clusters my code, this should be done inside the Write(), Writeln(), etc. functions. I cannot think of any case where I would want Aspose.Words to throw if the value is NULL. If I want to throw, I will do it myself.

I’d much rather write this:

builder.Writeln(obj.Description);

than this:

if (obj.Description != null) builder.Writeln(obj.Description);

or this:

builder.Writeln(obj.Description ?? "");

everywhere in my code.

Thanks.

@dstj,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-18438. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

@awais.hafeez

Thanks, and if, for any reason, you are hesitant to perform the NULL check in your code, you could also add an ON/OFF toggle for that NULL handling. Something like:

builder.NullWritingHandling = NullHandlingOption.Ignore;  // or NullHandlingOption.Throw

@dstj,

We have logged these details in our issue tracking system and will keep you posted on any further updates.

@dstj,

Regarding WORDSNET-18438, you can easily create and use your own method, something like this:

internal class MyClass
{
    internal MyClass()
    {
        Document doc = new Document();
        mDocBuilder = new DocumentBuilder(doc);
        WriteSafe(null);
    }

    private void WriteSafe(string text)
    {
        if (text != null)
            mDocBuilder.Write(text);
    }

    private DocumentBuilder mDocBuilder;
}

In this case, there is no need to insert null-check each time on writing. Hope, this helps.

@awais.hafeez. Yes, yes, wrapper functions around Write(), Writeln(), InsertHtml(), etc. works as workaround (though I’d write extension methods), but I believe my suggestion is still valid and that that should be handled in Aspose.Words itself.

@dstj,

We have logged these details in our issue tracking system and will keep you posted on any further updates.