0

How can I manage this error? I have a button that dequeued my enqueued objects and then save it in my TempData["QueueNumber"] then I have a call screen view which get the properties of my TempData and render it. It's partly working only when I have an object my TempData but when it's null its starts to throw me the Object reference not set to an instance of an object. error.

Here is my view:

@{
    var item = (Rosh.QueueMe.Web.Models.MyQueue)TempData["QueueItem"];
}

    <table id="auto">
        <tr style="font-family:'Arial Rounded MT'">
            <th class="tickets">TICKETS</th>
            <th class="name">NAME</th>
            <th class="counter">COUNTER</th>
            <th class="service">SERVICE</th>
        </tr>
        <tr class="data">
            <td>#@item.QueueNumber</td>
            <td>@item.Name</td>
            <td>Desk 1</td>
            <td>@item.ServiceId</td>
        </tr>
    </table>
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Carlo Toribio
  • 137
  • 11
  • As for your problem, add a null check before you use it. – ProgrammingLlama May 28 '19 at 06:24
  • I tried to add a null check but it throws me another error that my `@item.QueueNumber` does not exist in the current context – Carlo Toribio May 28 '19 at 06:26
  • You might want to have a dummy object as a substitute when `TempData` or `TempData["QueueItem"]` is null, or just not show that entire section. It's up to you. – ProgrammingLlama May 28 '19 at 06:27
  • To save the enqueued object use the session. when the page render the tempdata content will be null. – Vinutha N May 28 '19 at 06:28
  • my point is, I don't care if its null because it's supposed to be null at first . I just want my view to return even tho its null. – Carlo Toribio May 28 '19 at 06:30
  • See my previous comment. You _do_ care if it's null, because your view can't be rendered if it's null. That means you need to take some kind of alternative action in your view in the instance where it is null, so that the view can still be rendered. I've proposed a couple of solutions in my previous comment. – ProgrammingLlama May 28 '19 at 06:35
  • how can I make a dummy object? im sorry didnt see the comment – Carlo Toribio May 28 '19 at 06:37
  • To clarify: is `TempData` null here, or is `TempData["QueueItem"]` null? – ProgrammingLlama May 28 '19 at 06:40
  • Anyway, in principle, outside your `if` statement null check, define `Rosh.QueueMe.Web.Models.MyQueue item = null;`, and then if nothing is null assign `item = (Rosh.QueueMe.Web.Models.MyQueue)TempData["QueueItem"];`, otherwise (else statement) assign `item = // create a new instance of Rosh.QueueMe.Web.Models.MyQueue`. – ProgrammingLlama May 28 '19 at 06:49
  • `@if(TempData["QueueItem"] != null) { var item = (Rosh.QueueMe.Web.Models.MyQueue)TempData["QueueItem"]; }` that is my null check on my view what will I do? – Carlo Toribio May 28 '19 at 08:03
  • Sorry I only just saw this. Something like this: https://pastebin.com/gxZbLkQc – ProgrammingLlama May 29 '19 at 01:07
  • when I put `@if (TempData["QueueItem"] != null)` it's giving me an error on my view that my `@item.QueueNumber` is not in the current context. I tried the code you gave me, still have the same error. – Carlo Toribio May 29 '19 at 01:35

0 Answers0