[英]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. 如果查看Where或Single的文档,则会看到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 = null
或viewModel.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.