title: ASP.NET C# System.NullReferenceException on requesting cookies
body: <p>I have a problem when I request cookies, but I don't understand what I can do to solve it.</p>
  
  <p>My code:</p>
  
  <pre><code>public partial class Admin_LogIn : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (Request.Cookies["IvoucherCookieAdminPassword"] != null &amp;&amp; Request.Cookies["IvoucherCookieAdminMail"] != null)
              if ("***".Equals(Request.Cookies["IvoucherCookieAdminPassword"].Value.ToString()) &amp;&amp; "***".Equals(Request.Cookies["IvoucherCookieAdminMail"].Value.ToString()))
                  Response.Redirect("Benvenuto.aspx");
      }
      protected void LoginButton_Click(object sender, EventArgs e)
      {
          if (UserTextBox.Text == "***" &amp;&amp; PasswordTextBox.Text == "***")
          {
              Response.Cookies["IvoucherCookieAdminMail"].Value = UserTextBox.Text;
              Response.Cookies["IvoucherAdminCookiePassword"].Value = PasswordTextBox.Text;
              Response.Cookies["IvoucherCookieAdminMail"].Expires = DateTime.Now.AddHours(2);
              Response.Cookies["IvoucherAdminCookiePassword"].Expires = DateTime.Now.AddHours(2);
              Response.AddHeader("REFRESH", "0.1;Benvenuto.aspx");
          }
          else
          {
              GeneralErrorTextBox.Text = "Nome utente e/o password errata!";
          }
      }
  }
  </code></pre>
  
  <p>So I have tried this to fix it:</p>
  
  <pre><code>public partial class Admin_LogIn : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          var adminuser = Request.Cookies["IvoucherCookieAdminMail"];
          var adminpassword = Request.Cookies["IvoucherAdminCookiePassword"];
          if (adminuser != null &amp;&amp; adminpassword != null)
              if ("***".Equals(Request.Cookies["IvoucherCookieAdminPassword"].Value.ToString()) &amp;&amp; "***".Equals(Request.Cookies["IvoucherCookieAdminMail"].Value.ToString()))
                  Response.Redirect("Benvenuto.aspx");
      }
      protected void LoginButton_Click(object sender, EventArgs e)
      {
          if (UserTextBox.Text == "***" &amp;&amp; PasswordTextBox.Text == "***")
          {
              Response.Cookies["IvoucherCookieAdminMail"].Value = UserTextBox.Text;
              Response.Cookies["IvoucherAdminCookiePassword"].Value = PasswordTextBox.Text;
              Response.Cookies["IvoucherCookieAdminMail"].Expires = DateTime.Now.AddHours(2);
              Response.Cookies["IvoucherAdminCookiePassword"].Expires = DateTime.Now.AddHours(2);
              Response.AddHeader("REFRESH", "0.1;Benvenuto.aspx");
          }
          else
          {
              GeneralErrorTextBox.Text = "Nome utente e/o password errata!";
          }
      }
  }
  </code></pre>
  
  <p>But I always get the following error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
    
    <p>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>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
    
    <p>Stack Trace: </p>
  
  <pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
      Admin_LogIn.Page_Load(Object sender, EventArgs e) +126
      System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
      System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
      System.Web.UI.Control.OnLoad(EventArgs e) +91
      System.Web.UI.Control.LoadRecursive() +74
      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
  </code></pre>
    
    <p>Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1016</p>
  </blockquote>
  
  <p>How can I solve it?</p>