简体   繁体   中英

ASP.NET IIS server warning Application Error NullReferenceException

I have MVC project.

It works fine, but sometimes it's throws an exception.

Here is an IIS screenshot of error. 在此处输入图片说明

object reference not set to an instance of an object

object reference not set to an instance of an object

When the users reload the page, error disappears.

   @if (Model.CashboxStatus != null && Model.CashboxStatus.Count > 0)
   {
       foreach (var item in Model.CashboxStatus)
       {
           <td>@item.CashboxName</td>
       }
   }

this is the code populating the Model:

public ActionResult Cashboxes()
{
    CashboxesModel model = new CashboxesModel();
    model.Cashboxes = CashboxLogic.GetCashboxes();
    return View(model);
}

It gives an error on first line.

I have no idea why this error appears. We have about 100 users daily.

Any ideas?

Try this:

 @if (Model.CashboxStatus != null && Model.CashboxStatus.Count > 0)
   {
       foreach (var item in Model.CashboxStatus)
       {
           <td>@item.CashboxName ?? string.Empty</td>
       }
   }

Let me know if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM