0

I have MVC project.

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

Here is an IIS screenshot of error. enter image description here

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?

Eulogy
  • 197
  • 1
  • 1
  • 14
  • Have you verified that `Model` is not null? – CodeZombie Jan 20 '16 at 16:38
  • when i debuging my source Model is not null – Arsen Petrosyan Jan 20 '16 at 16:41
  • well, something in your model is null. you haven't provided enough details to resolve this. all you can do is put in additional logging and go through the data the user is attempting to look at and try to reproduce the issue. Also, don't put in screenshots of code or errors - copy the text itself into your question. – user1666620 Jan 20 '16 at 16:41
  • if its null,why refreshing the page is already works – Arsen Petrosyan Jan 20 '16 at 16:44
  • it could be something has timed out and the refresh is creating a new session. we can't tell because you haven't provided enough information. – user1666620 Jan 20 '16 at 16:45
  • what information do you need user1666620 ? – Arsen Petrosyan Jan 20 '16 at 16:49
  • what is the user doing? is there any time lag between when they request the page and see the error when they experience the error? show the code that is populating the model? is a new dbcontext created when the page is requested and disposed of immediately after use, or does it hang around for a while? what is your server configuration? is it possible there is an exception within the method that populates the model, so that when the view is compiled at runtime the model is null? what data is the user attempting to view? – user1666620 Jan 20 '16 at 16:53
  • user only open the page,,, – Arsen Petrosyan Jan 20 '16 at 16:59
  • @ArsenPetrosyan provide the code for the `CashboxLogic.GetCashboxes()` method. wrap the logic in `ActionResult Cashboxes()` in a a try/catch and log the exception to a text file. – user1666620 Jan 20 '16 at 17:10
  • You posted this yesterday... – Jamie Rees Jan 20 '16 at 17:23
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jamie Rees Jan 20 '16 at 17:23
  • @JamieR, I think the problem in this post is more specified. Also, he is not questioning about "What is a NullReferenceException" – Eulogy Jan 20 '16 at 17:51
  • @Eulogy The question is, I am getting a NullReferenceException, How do I fix it. – Jamie Rees Jan 20 '16 at 21:13
  • @JamieR, you are right, he needs to know what is trowing the NullReferenceException, not the meaning of NullReferenceException. – Eulogy Jan 21 '16 at 01:30

1 Answers1

0

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

Eulogy
  • 197
  • 1
  • 1
  • 14
  • my code work in local ,,,but sometimes it's throws an exception in SERVER,,,i will testing your code in server and will ask – Arsen Petrosyan Jan 20 '16 at 17:12
  • how does this solve the issue other than hiding an error? how do you know the problem is with that particular property and not the model as a whole? – user1666620 Jan 20 '16 at 17:13
  • The intention is not to hide the error, we need to know if the CashboxName property is null or not. The model has already been validated and has elements. Also, the downvote was necesary? if You can imvprove the answer, Do it. – Eulogy Jan 22 '16 at 20:51