处理 "object reference not set to an instance of an object" 错误
Handle "object reference not set to an instance of an object" error
我收到错误消息:
System.NullReferenceException occurred HResult=-2147467261
Message=Object reference not set to an instance of an object when
attempting to access a view.
错误指向这一行:
<span>@(d.AcceptPropagation == null ? string.Empty : "Accepts Propagation")</span>
我在 foreach 循环中设置了一个断点,然后逐步查找出现 null 的位置。直到第 5 次通过循环我才收到错误,并且我在 DocumentTemplate 处得到一个空值。我以为我正在正确处理 null。我需要额外的空检查吗?我需要以不同的方式处理吗?下面是视图。我还在代码中将“”更改为string.Empty。
@if (criteria != null)
{
foreach (var d in criteria)
{
<tr>
<td style="width: 80px;">
<a class="blue button edit-template" data-reveal-id="edit-form" id="@d.ID">Edit</a>
</td>
<td>
<strong>@(d.DocumentTemplate == null ? string.Empty: d.DocumentTemplate.Name)</strong><br />
<div>@d.GroupCode</div>
<span>@d.PlanCode</span>
<span>@d.SubPlanCode</span>
<span>@d.ActionCode</span>
<span>@d.AdmitTerm</span>
<span>@d.AdmitTypeCode</span>
<span>@d.ProgramCode</span>
<span>@d.ResidentCode</span>
<span>@(d.V19 == true ? "V19" : string.Empty)</span>
<span>@(d.A23 == true ? "A23" : string.Empty)</span>
<span>@(d.AcceptPropagation == null ? string.Empty : "Accepts Propagation")</span>
<div><a href="@Url.Content(d.DocumentTemplate.Path)">@(Url.Content(d.DocumentTemplate.Path)==null? string.Empty : d.DocumentTemplate.Path)</a></div>
</td>
</tr>
}
}
您没有检查此行中的 DocumentTemplate 是否为 null
<div><a href="@Url.Content(d.DocumentTemplate.Path)">@(Url.Content(d.DocumentTemplate.Path)==null? string.Empty : d.DocumentTemplate.Path)</a></div>
实际上是您提到的导致问题的行下方的行。由于某种原因,Razor 视图 NullReferenceExceptions 多次指向上面的行。将其下面的行更改为具有相同的空检查,它将起作用:
<div>
@if(d.DocumentTemplate != null)
{
<a href="@Url.Content(d.DocumentTemplate.Path)">@d.DocumentTemplate.Path</a>
}
</div>
查看 this link 用户最初发现问题的位置。
我收到错误消息:
System.NullReferenceException occurred HResult=-2147467261 Message=Object reference not set to an instance of an object when attempting to access a view.
错误指向这一行:
<span>@(d.AcceptPropagation == null ? string.Empty : "Accepts Propagation")</span>
我在 foreach 循环中设置了一个断点,然后逐步查找出现 null 的位置。直到第 5 次通过循环我才收到错误,并且我在 DocumentTemplate 处得到一个空值。我以为我正在正确处理 null。我需要额外的空检查吗?我需要以不同的方式处理吗?下面是视图。我还在代码中将“”更改为string.Empty。
@if (criteria != null)
{
foreach (var d in criteria)
{
<tr>
<td style="width: 80px;">
<a class="blue button edit-template" data-reveal-id="edit-form" id="@d.ID">Edit</a>
</td>
<td>
<strong>@(d.DocumentTemplate == null ? string.Empty: d.DocumentTemplate.Name)</strong><br />
<div>@d.GroupCode</div>
<span>@d.PlanCode</span>
<span>@d.SubPlanCode</span>
<span>@d.ActionCode</span>
<span>@d.AdmitTerm</span>
<span>@d.AdmitTypeCode</span>
<span>@d.ProgramCode</span>
<span>@d.ResidentCode</span>
<span>@(d.V19 == true ? "V19" : string.Empty)</span>
<span>@(d.A23 == true ? "A23" : string.Empty)</span>
<span>@(d.AcceptPropagation == null ? string.Empty : "Accepts Propagation")</span>
<div><a href="@Url.Content(d.DocumentTemplate.Path)">@(Url.Content(d.DocumentTemplate.Path)==null? string.Empty : d.DocumentTemplate.Path)</a></div>
</td>
</tr>
}
}
您没有检查此行中的 DocumentTemplate 是否为 null
<div><a href="@Url.Content(d.DocumentTemplate.Path)">@(Url.Content(d.DocumentTemplate.Path)==null? string.Empty : d.DocumentTemplate.Path)</a></div>
实际上是您提到的导致问题的行下方的行。由于某种原因,Razor 视图 NullReferenceExceptions 多次指向上面的行。将其下面的行更改为具有相同的空检查,它将起作用:
<div>
@if(d.DocumentTemplate != null)
{
<a href="@Url.Content(d.DocumentTemplate.Path)">@d.DocumentTemplate.Path</a>
}
</div>
查看 this link 用户最初发现问题的位置。