title: ASP.NET "'Session' threw an exception of type 'System.NullReferenceException'" master page
body: <p>I'm building a website with a shopping cart.<br>
  I want all Cart's Item will appear on master page.</p>
  
  <p>This is my SiteMaster.Master.cs:</p>
  
  <pre><code>  protected void Page_Load(object sender, EventArgs e)
      {
          System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser();
  
          IBLClient b = new FrontEnd();
          if (user != null)
              Session["UserID"] = b.GetUserIDByUsername(user.UserName);
  
          if (Session["Cart"] == null)
          {
              Session["Cart"] = new List&lt;string&gt;();
          }
  
          if (!(Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Seller")))
          {
              ManagePage.Visible = false;
  
          }
  
      }
  
  
      public BindingList&lt;Product&gt; GetProductsCart()
      {
          IBLClient b = new FrontEnd();
          BindingList&lt;Product&gt; ProductsList = new BindingList&lt;Product&gt;();
  
  
          if (Session["Cart"] != null)
          {
              foreach (string ProductId in (List&lt;string&gt;)Session["Cart"])
              {
                  ProductsList.Add(b.GetProduct(int.Parse(ProductId)));
              }
          }
  
          return ProductsList;
      }
  </code></pre>
  
  <p>And this is my Source code:</p>
  
  <pre><code> &lt;asp:DataList ID="ProductsList" runat="server" CellPadding="4" DataSourceID="ProductsListObjectDataSource" ForeColor="#333333"&gt;
                                      &lt;AlternatingItemStyle BackColor="White" /&gt;
                                      &lt;ItemTemplate&gt;
                                      &lt;/ItemTemplate&gt;
                                      &lt;SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /&gt;
                                  &lt;/asp:DataList&gt;
                                  &lt;asp:ObjectDataSource ID="ProductsListObjectDataSource" runat="server" SelectMethod="GetProductsCart" TypeName="WebGUI.SiteMaster"&gt;&lt;/asp:ObjectDataSource&gt;
  </code></pre>
  
  <p>When the Page is load it's look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/bKl7a.png" alt="enter image description here"></p>
  
  <p>But after the <code>ObjectDataSource</code>'s <code>SelectMethod</code> GetProductsCart() function, I got this exeption and the <code>this</code> changed.</p>
  
  <p><img src="https://i.stack.imgur.com/XbolJ.png" alt="enter image description here"></p>
  
  <p>Any idea why it's happening?<br>
  What should I do to avoid it?</p>