How to fix 'System.NullReferenceException: Object reference not set to an instance of an object.' ?

Kavya Kohli 0 Reputation points
2023-06-26T15:57:28.4166667+00:00
@model IEnumerable<MVC_CRUD.Models.Employee>

@{
    ViewData["Title"] = "Index";
}

<h4>Employee Register</h4>
<hr />
<table class="table table-hover">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.FullName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.EmpCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Position)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.OfficeLocation)
            </th>
            <th>
                <a asp-action="AddOrEdit" class="btn btn-outline-success"><i class="far fa-plus-square"></i> Employee</a>
            </th>
        </tr>
    </thead>
    <tbody>
@foreach (var  item in Model)    //Error comes in this line
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.FullName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.EmpCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Position)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.OfficeLocation)
                </td>
                <td>
                    <a asp-action="AddOrEdit" asp-route-id="@item.EmployeeId"><i class="fa fa-marker fa-lg"></i></a>
                    <a asp-action="Delete" asp-route-id="@item.EmployeeId" class="text-danger ml-1" onclick="return confirm('Are you sure to delete this record?')"><i class="fa fa-trash-alt fa-lg"></i></a>
                </td>
            </tr>
        }
    </tbody>
</table>


Developer technologies | ASP.NET | ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Srinivasulu Mallisetty 75 Reputation points
    2023-06-26T22:02:43.84+00:00

    View is receiving the Model value as Null. Debug the respective controller action to verify why is it returning null.

    If it is intended to return null, handle by using the Null-Coalescing Operator like below.

            @foreach (var item in Model)    //Error comes in this line
    
            @foreach (var item in Model ?? Enumerable.Empty<MVC_CRUD.Models.Employee>())    // Update it like this to handle null
    
    0 comments No comments

  2. Anonymous
    2023-06-27T02:16:02.2966667+00:00

    Hi @Kavya Kohli

    How to fix 'System.NullReferenceException: Object reference not set to an instance of an object.' ?

    Before using the foreach statement, you can add an If-Else statement to check whether the Model is null. Try to change the table body as below: if the model is null, it will show an "empty" in the table, otherwise display the records.

        <tbody>
            @if(Model == null)
            {
                <tr><td colspan="5" style="text-align:center">empty</td></tr>
            }
            else
            { 
                @foreach (var item in Model)     
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.FullName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.EmpCode)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Position)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.OfficeLocation)
                        </td>
                        <td>
                            <a asp-action="AddOrEdit" asp-route-id="@item.EmployeeId"><i class="fa fa-marker fa-lg"></i></a>
                            <a asp-action="Delete" asp-route-id="@item.EmployeeId" class="text-danger ml-1" onclick="return confirm('Are you sure to delete this record?')"><i class="fa fa-trash-alt fa-lg"></i></a>
                        </td>
                    </tr>
                }
            }
        </tbody>
    

    Like this:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.