Click here to Skip to main content
15,672,129 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server Error in '/' Application.
Object reference not set to an instance of an object.
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 68: <!-- /SECTION -->
Line 69: <div class="row product-container">
Line 70:     @foreach (var item in Model.ListOfProducts)
Line 71:     {
Line 72:         <div class="col-md-3 col-sm-3 col-xs-6" style="margin-bottom:8px">

What I have tried:

@model metro1.Models.Home.HomeIndexViewModel
@using PagedList;
@using PagedList.Mvc;
    ViewBag.Title = "Shop";
    Layout = "~/Views/Shared/_Layout.cshtml";


<div class="row product-container">
    @foreach (var item in Model.ListOfProducts)
        <div class="col-md-3 col-sm-3 col-xs-6" style="margin-bottom: 8px">
            <div class="thumbnail product-item" style="height: 300px">


                <div class="caption">
                    <p>@item.price $.</p>
                        @if (item.Quantity > 0)
                            using (Html.BeginForm("AddToCart", "Home", new { productId = item.ProductId }, FormMethod.Post))
                        <p>Not Available</p>

                        <div class="product-item-badge">
                            @if (item.IsFeatured == true)


<div class="container">
    @Html.PagedListPager(Model.ListOfProducts, page => Url.Action("Index", new { page, search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayItemSliceAndTotal = true })
Updated 29-Mar-20 21:37pm
F-ES Sitecore 30-Mar-20 5:41am    
Either Model is null or Model.ListOfProducts is null. It's impossible to say why from the code you've posted. Use the debugger to step through your action to see why you aren't returning any model data.

1 solution

System.NullReferenceException means that an instance in your code was null. You forgot to perform a new on the object and passed the default value in the code.

There are multiple ways in which you can prevent this error, the most basic and recommended approach is to create the instances properly.
var obj = new Type();
Where exactly you are getting the error starts from the foreach loop in your code and goes all the way down. From your question, I can tell perhaps this is null,
@foreach (var item in Model.ListOfProducts)
You can fix the problem with this:
@if(Model.ListOfProducts != null) {
    foreach (...) { }
} else {
    <p>The product list is null. Check the controller where it is being passed.</p>
But this does not solve the problem, does it? It will merely prevent the error from happening on the runtime. The good solution is to make sure that Model receives the value in ListOfProducts from the controller. You need to check if you are passing a null to View() function call.

Here are a few of my previous answers that discuss this problem and its solution in different contexts:
Null reference exception[^]
Problem with 'Null Reference' Error while creating Class instances[^]
how to handle null reference and out of bound exceptions[^]
Share this answer
MadMyche 29-Mar-20 20:40pm    
Afzaal Ahmad Zeeshan 30-Mar-20 2:55am    
Thank you.
Sajeda Alatiyat 30-Mar-20 7:36am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900