title: MVC5 JSON list NullReference exception
body: <p>I want to deserialize some JSON data and pass it to view in my ASP.NET MVC 5 app.</p>
  
  <p>My model:</p>
  
  <pre><code>public class OrderModel
  {
      public string Seat { get; set; }
      public string objectId { get; set; }
      public DateTime? createdAt { get; set; }
  
  }
  </code></pre>
  
  <p>I've read that to deserialize JSON data to list of object i have to make "root" class which is here:</p>
  
  <pre><code>public class OrderRootModel
  {
      public List&lt;OrderModel&gt; OrderList { get; set; }
  
  }
  </code></pre>
  
  <p>Here is a method that gets JSON data in ApiModel class:</p>
  
  <pre><code>//ApiModel class
      public OrderRootModel GetOrderData()
          {
  
              string url = "https://api.parse.com/1/classes/Orders";
  
              OrderRootModel model = new OrderRootModel();
              model = JsonConvert.DeserializeObject&lt;OrderRootModel&gt;(getParseIdData(url));
              return model;
          }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>public ActionResult Index()
          {
              ApiModel model = new ApiModel();
  
              return View(model.GetOrderData()) ;
          }
  </code></pre>
  
  <p>My view(for sake of testing it's pretty much an empty page):</p>
  
  <pre><code>@model myCinema.Models.OrderRootModel
  @{
      ViewBag.Title = "Index";
      Layout = null;
  }
  
  &lt;h2&gt;Order list&lt;/h2&gt;
  
  @foreach (var item in Model.OrderList)
  {
      @item.objectId;
      @item.createdAt;
      @item.Seat
  }
  </code></pre>
  
  <p>Now after launching my app i get " Object reference not set to an instance of an object." on "@foreach (var item in Model.OrderList)" line. Can someone help me to solve this problem?</p>
  
  <p><strong>EDIT:</strong>
  To answer why my question is different than others - i understand what usually causes this exception, but i've clearly made a mistake in my code which i cannot find, therefore i'm asking you guys to help me find it. Also my List is inside root class</p>
  
  <p>I will provide rest of the code and JSON i recieve soon.</p>
  
  <p><strong>EDIT2:</strong>
  So in my GetOrderData on return line model appears to be null.</p>
  
  <p>Here is my api JSON string respose from getParseIdData method:</p>
  
  <pre><code>{"results":[{"Seat":"5","createdAt":"2015-11-29T18:50:15.320Z","objectId":"BsDSolYPsT","updatedAt":"2015-11-29T19:40:55.020Z"},{"Seat":"6","createdAt":"2015-12-02T22:31:36.892Z","objectId":"kQJ0R5TUvw","updatedAt":"2015-12-02T22:31:36.892Z"},{"Seat":"7","createdAt":"2015-12-02T22:31:40.261Z","objectId":"sVtdj3aipb","updatedAt":"2015-12-02T22:31:40.261Z"},{"Seat":"8","createdAt":"2015-12-02T22:31:43.082Z","objectId":"7oH2ySrDFH","updatedAt":"2015-12-02T22:31:43.082Z"}]}
  </code></pre>
  
  <p>Here is method you asked for </p>
  
  <pre><code>public string getParseIdData(string url)
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
          request.Headers.Add("X-Parse-Application-Id", id);
          request.Headers.Add("X-Parse-REST-API-KEY", key);
          try
          {
              WebResponse response = request.GetResponse();
              using (Stream responseStream = response.GetResponseStream())
              {
                  StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                  return reader.ReadToEnd();
              }
          }
          catch (WebException ex)
          {
              WebResponse errorResponse = ex.Response;
              using (Stream responseStream = errorResponse.GetResponseStream())
              {
                  StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                  String errorText = reader.ReadToEnd();
                  Console.WriteLine(errorText);
              }
              throw;
          }
      }
  </code></pre>