0

I'm having trouble with the instance of my object it gave me an error in which i don't understand

List<Event> events = parseResponse.Deserialize<List<Event>>(_responseAsString);
ViewBag.eventss = events;

html

<table id="eventist" border="0" cellspacing="0" cellpadding="0">
<thead>
    <tr>
        <th>
            event_key
        </th>
         <th>
            user_token
        </th>                
        <th>
            event_set_key
        </th>           
        <th>
            event_type
        </th>
         <th>
          event_date
        </th>
          <th>
            event_amount
        </th>
         <th>
            event_location_key
        </th>                
        <th>
            event_location_name
        </th>           
        <th>
            event_location_city
        </th>
         <th>
          event_location_state
        </th>
        <th>
            event_location_country
        </th>
         <th>
          event_acknowledged
        </th>
    </tr>
</thead>
<tbody>
<%List<StopMalaria.Models.Event> events= ViewBag.eventss;%>
<% foreach (var item in events)
   { %>

    <tr>
        <td>
            <%: item.event_key%>
        </td>
        <td>
              <%: item.user_token%>
        </td>               
        <td>
            <%: item.event_set_key%>
        </td>           
        <td>
            <%: item.event_type%>
        </td> 
         <td>
            <%: item.event_date%>
        </td>
        <td>
              <%: item.event_amount%>
        </td>               
        <td>
            <%: item.event_location_key%>
        </td>           
        <td>
            <%: item.event_location_name%>
        </td> 
         <td>
            <%: item.event_location_city%>
        </td>
        <td>
              <%: item.event_location_state%>
        </td>               
        <td>
            <%: item.event_location_country%>
        </td>           
        <td>
            <%: item.event_acknowledged%>
        </td>                   
    </tr>

<% } %>
</tbody>
</table>

So i did this. now it is saying [NullReferenceException: Object reference not set to an instance of an object.] and my <% foreach(var item in events) are highlighted red .

I already have a class with all of the element

   public class Event
   {
     public string event_key { get; set; }
     public string user_token { get; set; }
     public string event_set_key { get; set; }
    public string event_type { get; set; }
    public string event_date { get; set; }
    public string event_amount { get; set; }
    public string event_location_key { get; set; }
    public string event_location_name { get; set; }
    public string event_location_city { get; set; }
    public string event_location_state { get; set; }
    public string event_location_country { get; set; }
    public string event_acknowledged { get; set; }
 }
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Yusuf
  • 589
  • 4
  • 8
  • 20
  • 1
    You need to create a ViewModel first (call it EventListModel) and have property List EventList. Once you populate your list of events from Datasource it should work. – Yusubov Jun 14 '12 at 21:49
  • create a view from my getevent() ??? – Yusuf Jun 14 '12 at 21:51
  • 1
    Add new class to your asp.net mvc application Model folder, you may call it "EventListModel". Inside it declare elements that you want to display in your View (for example: formName, status, EventList) – Yusubov Jun 14 '12 at 21:55
  • is that wat i was doing..i have model called event inside that model i have class called event like you see in the above code – Yusuf Jun 14 '12 at 22:00
  • Does your parseResponse call return data without error ? Also try not put your event list inside ViewBag. It might be the reason why you getting error. – Yusubov Jun 14 '12 at 22:02
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 11 '13 at 03:37

1 Answers1

3

Add a viewmodel for your view, sample may look like. Also use this viewmodel in your action:

 public class EventListModel
    {
        public EventListModel()
        {
            EventList = new List<Event>();
        }

        public string FormId { get; set; }
        public string ProgramId { get; set; }
        public string FormName { get; set; }

        public IList<Event> EventList { get; set; }
    }
Yusubov
  • 5,650
  • 9
  • 29
  • 68