"Object reference not set to an instance of an object" 尝试在 foreach 循环中使用 if 语句时
"Object reference not set to an instance of an object" when trying to use if statement in a foreach loop
我有一个 table,它显示学生必须注册的课程。我有一个 foreach 循环遍历学生的所有注册
@foreach (var item in Model.Enrollments)
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Title) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Type) </td>
<td> @Html.DisplayFor(modelItem => item.Status) </td>
</tr>
}
现在一切正常,但当我尝试使用 if 语句仅显示具有特定类型的课程时
@foreach (var item in Model.Enrollments)
{
if (item.Course.Type == "Basic Core")
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Title) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Type) </td>
<td> @Html.DisplayFor(modelItem => item.Status) </td>
</tr>
}
}
我明白了
"Object reference not set to an instance of an object." error on line 115
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 113:{
Line 114:
Line 115: if (item.Course.Type == "Basic Core") {
Line 116: <tr>
Line 117: <td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
据我所知,foreach 循环会跳过任何空对象(如果有的话)。任何帮助将不胜感激。
这是我的控制器
public ActionResult Details(int? StudentID)
{
if (StudentID == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(StudentID);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
正如 Coder of Code 所指出的,您获得了一个项目对象,其中包含一个设置为 null 的 Course 对象。
由于项目对象不为空,因此它是从枚举中选取的。然后在检查内部 Course 对象时它抛出异常,因为它是空的。为了避免像这样在评估对象之前只检查 null:
@foreach (var item in Model.Enrollments)
{
if (item.Course != null && item.Course.Type == "Basic Core")
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Title) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Type) </td>
<td> @Html.DisplayFor(modelItem => item.Status) </td>
</tr>
}
}
如果需要,您还可以调试剃刀代码以查看发生了什么。
我有一个 table,它显示学生必须注册的课程。我有一个 foreach 循环遍历学生的所有注册
@foreach (var item in Model.Enrollments)
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Title) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Type) </td>
<td> @Html.DisplayFor(modelItem => item.Status) </td>
</tr>
}
现在一切正常,但当我尝试使用 if 语句仅显示具有特定类型的课程时
@foreach (var item in Model.Enrollments)
{
if (item.Course.Type == "Basic Core")
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Title) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Type) </td>
<td> @Html.DisplayFor(modelItem => item.Status) </td>
</tr>
}
}
我明白了
"Object reference not set to an instance of an object." error on line 115
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 113:{
Line 114:
Line 115: if (item.Course.Type == "Basic Core") {
Line 116: <tr>
Line 117: <td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
据我所知,foreach 循环会跳过任何空对象(如果有的话)。任何帮助将不胜感激。
这是我的控制器
public ActionResult Details(int? StudentID)
{
if (StudentID == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(StudentID);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
正如 Coder of Code 所指出的,您获得了一个项目对象,其中包含一个设置为 null 的 Course 对象。
由于项目对象不为空,因此它是从枚举中选取的。然后在检查内部 Course 对象时它抛出异常,因为它是空的。为了避免像这样在评估对象之前只检查 null:
@foreach (var item in Model.Enrollments)
{
if (item.Course != null && item.Course.Type == "Basic Core")
{
<tr>
<td> @Html.DisplayFor(modelItem => item.Course.CourseID) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Title) </td>
<td> @Html.DisplayFor(modelItem => item.Course.Type) </td>
<td> @Html.DisplayFor(modelItem => item.Status) </td>
</tr>
}
}
如果需要,您还可以调试剃刀代码以查看发生了什么。