简体   繁体   English

LINQ正在引发System.NullReferenceException

[英]LINQ is provoking a System.NullReferenceException

The related post for System.NullReferenceException doesn't help, please avoid linking that or marking as duplicate! System.NullReferenceException的相关文章无济于事,请避免链接或将其标记为重复项!

Code purpose: Check if the docType (a string variable) is present on a list of strings replaceEntitiesConfig.DocTypes . 代码用途:检查字符串列表replaceEntitiesConfig.DocTypes上是否存在docType (字符串变量)。

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. 使用LINQ语句,会引发异常,甚至在docType初始化之前也会引发异常。 Without it (using foreach instead), it works perfectly. 没有它(使用foreach),它会完美地工作。 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. 两种代码都应该以相同的方式工作(甚至ReSharper都建议我更改LINQ表达式的foreach),但是使用LINQ失败。

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; 对于无法正常工作的情况,它不会超出此行 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) 尽管它之前曾起作用(唯一的变化是在此行之后,程序逻辑是使用LINQ语句而不是foreach完成的)

Values: 价值观:

  • docType = "LE" (I see it from the way exception is not thrown, or in the case it's thrown, debugging the GetDocument method). docType =“ LE”(我从不引发异常的方式看到它,或者在引发异常的情况下,调试GetDocument方法)。
  • replaceEntitiesConfig.DocTypes = new List<string>(); replaceEntitiesConfig.DocTypes = new List<string>();

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. 在您的情况下,发生的情况是编译器推断出未使用string docType ,因此将其从代码中删除,然后编译器发现未同时使用DocumentResponseModel responseForDocType ,该string docType也已从代码中删除。 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. 但是一旦引入了实际使用responseForDocType的那一行,我们还必须拥有创建事物的那一行,然后我们就有了例外。

TL;DR - build in Debug configuration, and see the difference. TL; DR-内置于Debug配置中,并查看其区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM