简体   繁体   English

值不能为空,参数名称来源

[英]Value cannot be null, r nparameter name source

I am confused. 我很困惑。 I have two statements that are the same and one works and the other receives the error - value cannot be null. 我有两个相同的语句,一个有效,另一个收到错误- 值不能为null。 r nparameter name source. 参数名称来源。 From what I read I am receiving the error because something is null in my linq expression. 从我的阅读中我收到错误,因为我的linq表达式中某些内容为空。 However from what I can tell nothing is null. 但是,据我所知,没有什么是空的。

The first if statement works. 第一个if语句有效。 When a person selects 'Select' from a list of Burn Project a list of BurnPiles is displayed below the BurnProject list. 当某人从“刻录项目”列表中选择“选择”时,将在“刻录项目”列表下方显示“刻录文件”列表。 (this works). (这有效)。 Then when a person selects 'Select' from the list of BurnPiles a list of RequestedBurns is display below it. 然后,当某人从BurnPiles列表中选择“选择”时,将在其下方显示RequestedBurns列表。 This gives me the null error. 这给了我空错误。 At one time it did work now it doesn't. 一次它确实工作了,但现在却没有。

I am at a loss of what went wrong. 我不知道出了什么问题。 I do know the RequestedBurn Table starts at record #2 but that shouldn't have anything to do with it. 我确实知道RequestedBurn表从记录#2开始,但是那与它没有任何关系。 The BurnPile records that I have been using have associated RequestedBurns. 我一直在使用的BurnPile记录已关联RequestedBurns。

        //when 'Select' is chosen from the list of burn projects the list of burn piles 
        //associated with that specific burn project is display below it.
        if (burnerID != null)
        {
            ViewBag.BurnerID = burnerID.Value;
            viewModel.BurnPiles = viewModel.BurnProjects.Where(
                b => b.BurnerID == burnerID.Value).Single().BurnPiles;
        }

        //when 'Select' is chosen from the list of burn piles the list of requested 
        //burns associated with that specific burn pile is displayed below it.
        if (burnPileID != null)
        {
            ViewBag.BurnPilesID = burnPileID.Value;
            viewModel.RequestedBurns = viewModel.BurnPiles.Where(
                x => x.BurnPilesID == burnPileID).Single().RequestedBurns;

        }

If you look at documentation for Where or Single , you would see that source is the name of the parameter that represents your collection. 如果查看WhereSingle的文档,则会看到source是表示您的集合的参数的名称。 So, it looks like you are trying to call a method on null reference, which would be the case if viewModel.BurnProjects = null or viewModel.BurnPiles = null . 因此,似乎您正在尝试在null引用上调用方法,如果viewModel.BurnProjects = nullviewModel.BurnPiles = null就是这种情况。

viewModel.BurnPiles = viewModel.BurnProjects.Where(
            b => b.BurnerID == burnerID.Value).Single().BurnPiles;

could be setting viewModel.BurnPiles to null. 可以将viewModel.BurnPiles设置为null。

or 要么

viewModel.BurnPiles.Where(
            x => x.BurnPilesID == burnPileID).Single()

is returning nothing so when you try and access RequestedBurns then it throws an exception. 没有返回任何内容,因此当您尝试访问RequestedBurns时,它将引发异常。

SingleOrDefault also has an overload where you can simplify the expression a bit more. SingleOrDefault也有一个重载 ,您可以在其中进一步简化表达式。 You can also combine it with the null conditional operator (if using at least C# 6). 您还可以将其与空条件运算符结合在一起(如果至少使用C#6)。

if (burnerID != null)
{
    ViewBag.BurnerID = burnerID.Value;
    viewModel.BurnPiles = viewModel.BurnProjects.SingleOrDefault(b => b.BurnerID == burnerID.Value)?.BurnPiles;
}

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

相关问题 值不能为null或为空。\ r \ nParameter name:name - Value cannot be null or empty.\r\nParameter name: name 值不能为空。\\ r \\ n参数名称:输入 - Value cannot be null.\r\nParameter name: input “值不能为空。\\r\\n参数名称:文本” - “Value cannot be null.\r\nParameter name: text” {“值不能为空。\\ r \\ nParameter name:s”} - {“Value cannot be null.\r\nParameter name: s”} Image.Save() 抛出异常“值不能为空。/r/n 参数名称:编码器” - Image.Save() throws exception "Value cannot be null./r/nParameter name: encoder" Image.Save崩溃:{“值不能为空。\\ r \\ nParameter name:encoder”} - Image.Save crashing: {“Value cannot be null.\r\nParameter name: encoder”} C#Powershell-Exchange管理{“值不能为空。\\ r \\ n参数名称:serverSettings”} - C# Powershell - Exchange management {“Value cannot be null.\r\nParameter name: serverSettings”} Dynamics Crm 2015:值不能为空。\\ r \\ n参数名称:详细信息 - Dynamics Crm 2015: Value cannot be null.\r\nParameter name: detail “值不能为空。\\ r \\ n参数名称:实体(ASP.NET Web API) - "Value cannot be null.\r\nParameter name: entity (ASP.NET Web API) 根据xml错误{“值不能为空。\\ r \\ n参数名称:元素”}创建对象 - Creating objects from xml error {“Value cannot be null.\r\nParameter name: element”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM