LINQ is provoking a System.NullReferenceException

The related post for System.NullReferenceException doesn't help, please avoid linking that or marking as duplicate!

Code purpose: Check if the docType (a string variable) is present on a list of strings replaceEntitiesConfig.DocTypes.

Code that works:

private bool MustReplaceEntities(DocumentRequestModel docRequest, DocumentRequestOptions requestedInfo)
{
    var replaceEntitiesConfig = this.ConfigurationManager.GetModel<ReplaceEntitiesConfigModel>(ConfigurationModels.ContextualMenus, this.ApplicationContext.Application.ProductId);
    if (requestedInfo.Text && !replaceEntitiesConfig.DocTypes.IsEmpty())
    {
        var docType = this.GetDocument(docRequest, new DocumentRequestOptions { DocType = true }, DecoratorTypes.None).DocType;
        foreach (var configuredDocType in replaceEntitiesConfig.DocTypes)
        {
            if (configuredDocType.Equals(docType, StringComparison.InvariantCultureIgnoreCase))
            {
                return true;
            }
        }
    }
    return false;
}

Code that doesn't work:

private bool MustReplaceEntities(DocumentRequestModel docRequest, DocumentRequestOptions requestedInfo)
{
    var replaceEntitiesConfig = this.ConfigurationManager.GetModel<ReplaceEntitiesConfigModel>(ConfigurationModels.ContextualMenus, this.ApplicationContext.Application.ProductId);
    if (requestedInfo.Text && !replaceEntitiesConfig.DocTypes.IsEmpty())
    {
        var docType = this.GetDocument(docRequest, new DocumentRequestOptions { DocType = true }, DecoratorTypes.None).DocType;
        return replaceEntitiesConfig.DocTypes.Any(x => x.Equals(docType, StringComparison.InvariantCultureIgnoreCase));
    }
    return false;
}

With LINQ statement, exception is thrown, and the exception is thrown even before docType is initialized. Without it (using foreach instead), it works perfectly. Both codes should work the same way (even ReSharper suggests me to change the foreach for the LINQ expression), but with LINQ it's failing.

For the case that it doesn't work, it doesn't go any further than this line var docType = this.GetDocument(docRequest, new DocumentRequestOptions { DocType = true }, DecoratorTypes.None).DocType; though it worked before (the only change is that after this line the program logic is done with a LINQ statement instead of a foreach)

Values:

Upvotes: 1

Views: 241

Answers (1)

Dialecticus
Dialecticus

Reputation: 16779

You're probably building Release configuration, where some optimizations are in effect. In your case what happens is that compiler deduces that string docType is not used, so it removes it from code, and then compiler figures out that DocumentResponseModel responseForDocType is not used as well, which is also removed from code. Non-existent code does not cause exceptions.

But once one of the lines that actually uses responseForDocType is introduced, we also must have the line that creates the thing, and then we have the exception.

TL;DR - build in Debug configuration, and see the difference.

Upvotes: 1

Related Questions