简体   繁体   English

对象引用未设置为View中对象的实例

[英]Object reference not set to an instance of an object in View

I have following code in view(in javascript) 我在视图中有以下代码(在javascript中)

 console.log("@Model.Sides.SingleOrDefault(a => a.Name == "B").Surfaces.Count");

it throws an exception 引发异常

Object reference not set to an instance of an object 你调用的对象是空的

How to make that when an exception is thrown-nothing happened? 抛出异常时什么也没发生怎么办?

In your case there might be situation when there are no items in @Model.Sides In that case you can use. 在您的情况下, @Model.Sides可能没有任何项目,这种情况下您可以使用。

@if(Model.Sides.Any() && Model.Sides.SingleOrDefault(a => a.Name == "B").Any())
{
console.log("@Model.Sides.SingleOrDefault(a => a.Name == "B").Surfaces.Count");
}
else
{
console.log("0");
}

You just need to check your objects are initialized first. 您只需要检查对象是否首先被初始化即可。 This logic should really be outside of your views. 这种逻辑确实应该超出您的看法。

@{
    int count = 0;

    //Check the model isnt null first
    if (Model != null)
    {

        var side = Model.Sides.SingleOrDefault(a => a.Name == "B");

        if (side != null)
        {
            //You can perform null check on surfaces here as well
            count = side.Surfaces.Count;
        }

    }


}

<script>

    console.log("@count");

</script>

You can try the following 您可以尝试以下

@if(Model.Sides.SingleOrDefault(a => a.Name == "B").Any())
{
console.log("@Model.Sides.SingleOrDefault(a => a.Name == "B").Surfaces.Count");
}
else
{
console.log("0");
}

SingleOrDefault will return a null if the list does not contain exactly one item. 如果列表不完全包含一项,则SingleOrDefault将返回null。

If your 'where' clause a.Name == 'B' does not return exactly one item then the result will be null. 如果您的'where'子句a.Name == 'B'没有完全返回一项,则结果将为null。

So if your Sides list does not have just one 'B' (ie there are none or 2 or more than 2), the result of SingleOrDefault will be null and .Surfaces will give the error 'Object reference not set to an instance of an object`. 所以,如果身体两侧列表中不只有一个“B”(即有没有或2个或2个以上)的结果SingleOrDefault将是无效和.Surfaces会给错误“对象引用未设置为一个实例对象`。

If you simply want a count of zero when there isn't just one B side, then use: 如果您只想在不只有B面的情况下计数为零,请使用:

console.log("@(Model.Sides.SingleOrDefault(a => a.Name == "B") ?? new Side { Surfaces = new List<Surface>() }).Surfaces.Count");

where ?? 哪里?? means: if the left part is null, return this instead of null and you create a dummy Side with an empty list so that Surfaces.Count == 0 意思是:如果左边为空,则返回它而不是null,并创建一个带有空列表的虚拟Side ,以使Surfaces.Count == 0

Alternatively, use a variable: 或者,使用变量:

@{
    var side = Model.Sides.SingleOrDefault(a => a.Name == "B");
    if (side == null) 
    {
        @:console.log("none")
    } 
    else 
    {
        @:console.log(@side.Surfaces.Count);
    }
}

As a cleaner alternative, you could use SelectMany() instead, something like (untested) : 作为一种更清洁的选择,您可以改用SelectMany() ,例如(未测试):

Model.Sides.Where(a=>a.Name=="B").SelectMany(x=>x.Surfaces).Count()

then you don't need to worry about temp variables etc but will return the number of 'surfaces' for all 'B' sides, so may not be your requirement. 那么您不必担心温度变量等问题,但是会为所有“ B”侧返回“表面”的数量,因此可能不是您的要求。

On Catch(e) do nothing: 在Catch(e)上不执行任何操作:

This is generic that can suppress any error. 这是通用的,可以抑制任何错误。 Wrap the code you think can error with try catch, with catch doing nothing. 用try catch包装您认为可能会出错的代码,而catch什么都不做。

But this could lead into further problems, just be cautious with empty catch block. 但这可能会导致进一步的问题,只是对空的捕获块要谨慎。

ex: 例如:

try
  {
       //code that errors
  }

catch (e)
  {
      ;
  }

//Continue

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

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