Error during calling Model method in cshtml file

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello All.

I have created on new Index.cshtml file

In that I add @model MiniShoppingCartModel as first line.

Then I call methods as below :

        <div class="count">
            @if (Model.TotalProducts == 0)
            {
                @T("ShoppingCart.Mini.NoItems")
            }
            else
            {
                @Html.Raw(string.Format(T("ShoppingCart.Mini.ItemsText").Text, $"<a href=\"{Url.RouteUrl("ShoppingCart")}\">{string.Format(T("ShoppingCart.Mini.Items").Text, Model.TotalProducts)}</a>"))
            }
        </div>

Now when I run my page then getting following error :

+ $exception {"Object reference not set to an instance of an object."} System.NullReferenceException

Any idea how to solve this.

Thanks
6 years ago
Hello,

Object reference not set to an instance of an object. Means getting null value.

Whenever you are going to access any property from class and if class is null then you will get this error.

So you must need to handle null condition first.

Thanks,
Jatin
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.