title: Object reference not set to an instance of an object. While Adding items to Cart using Session in Asp.net mvc
body: <p>Hello Everyone I am trying to add items to cart using Session But I am facing That issue
  System.NullReferenceException: Object reference not set to an instance of an object.
  <a href="https://i.stack.imgur.com/qomzG.png" rel="nofollow noreferrer">Check This Image</a></p>
  <p>Here is my View Where I want to show Cart items</p>
  <pre><code> @foreach (var item in (List&lt;Product&gt;)Session[&quot;MyCart&quot;])
                      {
                          
                          &lt;li&gt;
                              &lt;a href=&quot;#&quot; class=&quot;photo&quot;&gt;&lt;img src=&quot;@Url.Content(item.Product_Picture)&quot; class=&quot;cart-thumb&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                              &lt;h6&gt;&lt;a href=&quot;#&quot;&gt;@item.Product_Name &lt;/a&gt;&lt;/h6&gt;
                              &lt;p&gt;1x -Rs &lt;span class=&quot;price&quot;&gt;@item.Product_SalePrice&lt;/span&gt;&lt;/p&gt;
                          &lt;/li&gt;
                       
                      }
                  &lt;/ul&gt;
  </code></pre>
  <p>Now This is my Action Controller</p>
  <pre><code>public ActionResult AddtoCart(int id)
      {
          List&lt;Product&gt; list;
          if (Session[&quot;MyCart&quot;] == null)
          { list = new List&lt;Product&gt;(); }
          else
          { list = (List&lt;Product&gt;)Session[&quot;MyCart&quot;]; }
          list.Add(db.Products.Where(p =&gt; p.Product_ID == id).FirstOrDefault());
          Session[&quot;MyCart&quot;] = list;
          return RedirectToAction(&quot;Shop&quot;);
      }
  </code></pre>