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:
- docType = "LE" (I see it from the way exception is not thrown, or in the case it's thrown, debugging the GetDocument method).
- replaceEntitiesConfig.DocTypes =
new List<string>();