title: Deserialize Json to class object using restsharp
body: <p>I have checked out the documentation for restsharp and also googled for a solution, but unable to find a solution for this problem:</p>
  
  <p>I have an asp.net mvc3 app and need to connect to quizlet.com to access public flashcards. I have added a reference to restsharp and created the following classes:</p>
  
  <pre><code>public class QuizletObject
  {
      public string response_type { get; set; }
      public int total_results { get; set; }
      public int page { get; set; }
      public int total_pages { get; set; }
      public int sets_with_images { get; set; }
      public Set[] sets { get; set; }
  }
  
  public class Set
  {
      public int id { get; set; }
      public string title { get; set; }
      public string url { get; set; }
      public string creator { get; set; }
      public int created { get; set; }
      public int term_count { get; set; }
      public bool has_images { get; set; }
      public int last_modified { get; set; }
  }
  </code></pre>
  
  <p>I have a class and method that calls the web service like this:</p>
  
  <pre><code>public class Quizlet
  {
      private string _baseUrl = "http://api.quizlet.com/1.0/sets";
      private const string DevKey = "my dev key";               
  
      public QuizletObject GetQuizletSets()
      {
          _baseUrl = _baseUrl + "?dev_key=" + DevKey + "&amp;q=" + "geography";
  
          RestClient client = new RestClient();
          client.BaseUrl = _baseUrl;
  
          RestRequest request = new RestRequest(Method.GET);
          request.RequestFormat = DataFormat.Json;
  
          RestResponse&lt;QuizletObject&gt; response = client.Execute&lt;QuizletObject&gt;(request);
  
          return response.Data;
      }
  }
  </code></pre>
  
  <p>When I try to access the method using the below code in the view, I get a <strong>NullReference exception (Object reference not set to an instance of an object).</strong></p>
  
  <pre><code>@{
  QuizletObject quizletObject = quizlet.GetQuizletSets();
  }
  
  @foreach (Set quizletSet in quizletObject.sets)
  {
      @quizletSet.id.ToString()
      &lt;br /&gt;
      @quizletSet.title
      &lt;br /&gt;
      @quizletSet.creator
      &lt;br /&gt;
      @quizletSet.created
      &lt;br /&gt;
      @Html.Encode(quizletSet.url)    
      &lt;br /&gt;
      &lt;br /&gt;
  }    
  </code></pre>
  
  <p>If the same method is executed as below, it returns the complete json object without any problem</p>
  
  <pre><code>RestResponse response = client.Execute(request);
  </code></pre>
  
  <p>Stack trace:</p>
  
  <pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
  ASP._Page_Views_Home_Index_cshtml.Execute() in c:\Users\Girish\Documents\Visual Studio 2010\Projects\RestSharpPoC\RestSharpPoC\Views\Home\Index.cshtml:18
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
  System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
  System.Web.WebPages.StartPage.RunPage() +19
  System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
  System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
  System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
  System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
  System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
  System.Web.Mvc.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() +23
  System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
  System.Web.Mvc.&lt;&gt;c__DisplayClass1e.&lt;InvokeActionResultWithFilters&gt;b__1b() +19
  System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
  System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
  System.Web.Mvc.Controller.ExecuteCore() +116
  System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
  System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
  System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +37
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +12
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
  System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +50
  System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +7
  System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +184
  </code></pre>