title: While compiling ASP.NET ecommerce website receive: Object reference not set to an instance of an object
body: <p>I just partially added shopping cart to my ecommerce test-site. </p>
  
  <p>It still can't do checkout, or even update quantity of items(about to start though) but I should be able to open the page responsible for displaying the shopping cart. Instead it bricks my entire website.</p>
  
  <p>I get the following error when compiling the site:</p>
  
  <blockquote>
    <p>Server Error in '/Website' Application. Object reference not set to an
    instance of an object. Description: An unhandled exception occurred
    during the execution of the current web request. Please review the
    stack trace for more information about the error and where it
    originated in the code.</p>
    
    <p>Exception Details: System.NullReferenceException: Object reference not
    set to an instance of an object.</p>
    
    <p>Source Error:</p>
    
    <p>The source code that generated this unhandled exception can only be
    shown when compiled in debug mode. To enable this, please follow one
    of the below steps, then request the URL:</p>
    
    <ol>
    <li><p>Add a "Debug=true" directive at the top of the file that generated the error. Example:</p>
    
    </li>
    </ol>
    
    <p>or:</p>
    
    <p>2) Add the following section to the configuration file of your
    application:</p>
    
    <p>    
                </p>
    
    <p>Note that this second technique will cause all files within a given
    application to be compiled in debug mode. The first technique will
    cause only that particular file to be compiled in debug mode.</p>
    
    <p>Important: Running applications in debug mode does incur a
    memory/performance overhead. You should make sure that an application
    has debugging disabled before deploying into production scenario.</p>
    
    <p>Stack Trace:</p>
    
    <p>[NullReferenceException: Object reference not set to an instance of an
    object.]    ShoppingCartAccess.get_shoppingCartId() +81<br>
    ShoppingCartAccess.GetItems() +57<br>
    UserControls_CartSummary.PopulateControls() +30<br>
    UserControls_CartSummary.Page_PreRender(Object sender, EventArgs e) +5
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object
    o, Object t, EventArgs e) +14<br>
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
    EventArgs e) +35    System.Web.UI.Control.OnPreRender(EventArgs e)
    +8991986    System.Web.UI.Control.PreRenderRecursiveInternal() +103    System.Web.UI.Control.PreRenderRecursiveInternal() +175<br>
    System.Web.UI.Control.PreRenderRecursiveInternal() +175<br>
    System.Web.UI.Control.PreRenderRecursiveInternal() +175<br>
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +2496</p>
    
    <p>Version Information: Microsoft .NET Framework Version:4.0.30319;
    ASP.NET Version:4.0.30319.237</p>
  </blockquote>
  
  <p>Judging from the stack trace error(and if I'm correct from judging the stack trace error) -  the error appears to happen with <strong>ShoppingCartAccess.get_shoppingCartId()</strong>. Threfore I'm including the code for the <strong>ShoppingCartAccess.cs</strong>:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Common;
  using System.Data;
  
  /// &lt;summary&gt;
  /// Summary description for ShoppingCartAccess
  /// &lt;/summary&gt;
  public class ShoppingCartAccess
  {
      public ShoppingCartAccess()
      {
          //
          // TODO: Add constructor logic here
          //
      }
  
      // returns the shopping cart ID for the current user
      private static string shoppingCartId
      {
          get
          {
              // get the current HttpContext
              HttpContext context = HttpContext.Current;
              // try to retrieve the cart ID from the user cookie
              string cartId = context.Request.Cookies["Website_CartID"].Value;
              // if the cart ID isn't in the cookie...
              {
                  // check if the cart ID exists as a cookie
                  if (context.Request.Cookies["Website_CartID"] != null)
                  {
                      // return the id
                      return cartId;
                  }
                  else
                  // if the cart ID doesn't exist in the cookie as well, generate
                  // a new ID
                  {
                      // generate a new GUID
                      cartId = Guid.NewGuid().ToString();
                      // create the cookie object and set its value
                      HttpCookie cookie = new HttpCookie("Website_CartID", cartId);
                      // set the cookie's expiration date
                      int howManyDays = WebsiteConfiguration.CartPersistDays;
                      DateTime currentDate = DateTime.Now;
                      TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0);
                      DateTime expirationDate = currentDate.Add(timeSpan);
                      cookie.Expires = expirationDate;
                      // set the cookie on the client's browser
                      context.Response.Cookies.Add(cookie);
                      // return the CartID
                      return cartId.ToString();
                  }
              }
          }
      }
  
      // Add a new shopping cart item
      public static bool AddItem(string productId, string attributes)
      {
          // get a configured DbCommand object
          DbCommand comm = GenericDataAccess.CreateCommand();
          // set the stored procedure name
          comm.CommandText = "ShoppingCartAddItem";
          // create a new parameter
          DbParameter param = comm.CreateParameter();
          param.ParameterName = "@CartID";
          param.Value = shoppingCartId;
          param.DbType = DbType.String;
          param.Size = 36;
          comm.Parameters.Add(param);
          // create a new parameter
          param = comm.CreateParameter();
          param.ParameterName = "@ProductID";
          param.Value = productId;
          param.DbType = DbType.Int32;
          comm.Parameters.Add(param);
          // create a new parameter
          param = comm.CreateParameter();
          param.ParameterName = "@Attributes";
          param.Value = attributes;
          param.DbType = DbType.String;
          comm.Parameters.Add(param);
          // returns true in case of success and false in case of an error
          try
          {
              // execute the stored procedure and return true if it executes
              // successfully, and false otherwise
              return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
          }
          catch
          {
              // prevent the exception from propagating, but return false to
              // signal the error
              return false;
          }
      }
  
  
      // Update the quantity of a shopping cart item
      public static bool UpdateItem(string productId, int quantity)
      {
          // get a configured DbCommand object
          DbCommand comm = GenericDataAccess.CreateCommand();
          // set the stored procedure name
          comm.CommandText = "ShoppingCartUpdateItem";
          // create a new parameter
          DbParameter param = comm.CreateParameter();
          param.ParameterName = "@CartID";
          param.Value = shoppingCartId;
          param.DbType = DbType.String;
          param.Size = 36;
          comm.Parameters.Add(param);
          // create a new parameter
          param = comm.CreateParameter();
          param.ParameterName = "@ProductID";
          param.Value = productId;
          param.DbType = DbType.Int32;
          comm.Parameters.Add(param);
          // create a new parameter
          param = comm.CreateParameter();
          param.ParameterName = "@Quantity";
          param.Value = quantity;
          param.DbType = DbType.Int32;
          comm.Parameters.Add(param);
          // returns true in case of success and false in case of an error
          try
          {
              // execute the stored procedure and return true if it executes
              // successfully, and false otherwise
              return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
          }
          catch
          {
              // prevent the exception from propagating, but return false to
              // signal the error
              return false;
          }
      }
  
      // Remove a shopping cart item
      public static bool RemoveItem(string productId)
      {
          // get a configured DbCommand object
          DbCommand comm = GenericDataAccess.CreateCommand();
          // set the stored procedure name
          comm.CommandText = "ShoppingCartRemoveItem";
          // create a new parameter
          DbParameter param = comm.CreateParameter();
          param.ParameterName = "@CartID";
          param.Value = shoppingCartId;
          param.DbType = DbType.String;
          param.Size = 36;
          comm.Parameters.Add(param);
          // create a new parameter
          param = comm.CreateParameter();
          param.ParameterName = "@ProductID";
          param.Value = productId;
          param.DbType = DbType.Int32;
          comm.Parameters.Add(param);
          // returns true in case of success and false in case of an error
          try
          {
              // execute the stored procedure and return true if it executes
              // successfully, and false otherwise
              return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
          }
          catch
          {
              // prevent the exception from propagating, but return false to
              // signal the error
              return false;
          }
      }
  
      // Retrieve shopping cart items
      public static DataTable GetItems()
      {
          // get a configured DbCommand object
          DbCommand comm = GenericDataAccess.CreateCommand();
          // set the stored procedure name
          comm.CommandText = "ShoppingCartGetItems";
          // create a new parameter
          DbParameter param = comm.CreateParameter();
          param.ParameterName = "@CartID";
          param.Value = shoppingCartId;
          param.DbType = DbType.String;
          param.Size = 36;
          comm.Parameters.Add(param);
          // return the result table
          DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
          return table;
      }
  
      // Retrieve shopping cart items
      public static decimal GetTotalAmount()
      {
          // get a configured DbCommand object
          DbCommand comm = GenericDataAccess.CreateCommand();
          // set the stored procedure name
          comm.CommandText = "ShoppingCartGetTotalAmount";
          // create a new parameter
          DbParameter param = comm.CreateParameter();
          param.ParameterName = "@CartID";
          param.Value = shoppingCartId;
          param.DbType = DbType.String;
          param.Size = 36;
          comm.Parameters.Add(param);
          // return the result table
          return Decimal.Parse(GenericDataAccess.ExecuteScalar(comm));
      }
  
  }
  </code></pre>
  
  <p>Where did I go wrong?</p>