title: Object reference not set to an instance of an object in Remember Me
body: <p>I am writing code to store cookies for Remember Me next time in Login control. Which this will store the user's username on the textbox if the user had checked the "Remember Me" check box. And my login control was set inside LoginView.  </p>
  
  <p>When i run the program and fill in the fields in the login control and hit submit(with or without checking the check box), it gave me this error: </p>
  
  <pre><code>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  
  Source Error: 
  
  Line 65: 
  Line 66:         HttpCookie myCookie = new HttpCookie("myCookie");
  Line 67:         if (RememberMe.Checked == true) //here is the line giving error
  Line 68:         {
  Line 69:             myCookie.Values.Add("username", Login1.UserName);
  
  Line: 67 
  </code></pre>
  
  <p>This is the code for login control: </p>
  
  <pre><code> &lt;asp:LoginView ID="LoginView1" runat="server"&gt;
          &lt;AnonymousTemplate&gt; 
          &lt;asp:Login ID="Login1" runat="server" onloggingin="Login1_LoggingIn" 
                  onloginerror="Login1_LoginError" onauthenticate="Login1_Authenticate" 
                  RememberMeSet="True"&gt;
              &lt;LayoutTemplate&gt;
                  &lt;table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"&gt;
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;table cellpadding="0"&gt;
                                  &lt;tr&gt;
                                      &lt;td align="center" colspan="2"&gt;
                                          Log In&lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                      &lt;td align="right"&gt;
                                          &lt;asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"&gt;User Name:&lt;/asp:Label&gt;
                                      &lt;/td&gt;
                                      &lt;td&gt;
                                          &lt;asp:TextBox ID="UserName" runat="server"&gt;&lt;/asp:TextBox&gt;
                                          &lt;asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                              ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                              ToolTip="User Name is required." ValidationGroup="ctl00$Login1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                      &lt;td align="right"&gt;
                                          &lt;asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"&gt;Password:&lt;/asp:Label&gt;
                                      &lt;/td&gt;
                                      &lt;td&gt;
                                          &lt;asp:TextBox ID="Password" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                                          &lt;asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                              ControlToValidate="Password" ErrorMessage="Password is required." 
                                              ToolTip="Password is required." ValidationGroup="ctl00$Login1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                      &lt;td colspan="2"&gt;
                                          &lt;asp:CheckBox ID="RememberMe" runat="server" 
                                              Text="Remember me next time." /&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                      &lt;td align="center" colspan="2" style="color:Red;"&gt;
                                          &lt;asp:Literal ID="FailureText" runat="server" EnableViewState="False"&gt;&lt;/asp:Literal&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                                  &lt;tr&gt;
                                      &lt;td align="right" colspan="2"&gt;
                                          &lt;asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                                              ValidationGroup="ctl00$Login1" /&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/LayoutTemplate&gt;
          &lt;/asp:Login&gt;
        &lt;/AnonymousTemplate&gt;
  
        &lt;LoggedInTemplate&gt; 
            &lt;asp:LoginStatus ID="LoginStatus1" runat="server" /&gt;
  
        &lt;/LoggedInTemplate&gt;
          &lt;/asp:LoginView&gt;
  </code></pre>
  
  <p>This is the code at the back: </p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
      TextBox UserName = (TextBox)Login1.FindControl("UserName");
  
  
      if (Request.Cookies["myCookie"] != null)
      {
  
          HttpCookie cookie = Request.Cookies.Get("myCookie");
          Login1.UserName = cookie.Values["username"];
  
          //.Attributes.Add("value", cookie.Values["password"]);
          Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);
  
  
      }
  } 
  
  protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
  {
       System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
      TextBox UserName = (TextBox)Login1.FindControl("UserName");
  
      //Check to see if the current user exists
      if (Membership.GetUser(Login1.UserName) != null)
      {
          //Check to see if the user is currently locked out
          if (Membership.GetUser(Login1.UserName).IsLockedOut)
          {
              //Get the last lockout  date from the user
              DateTime lastLockout = Membership.GetUser(Login1.UserName).LastLockoutDate;
              Response.Write(lastLockout.ToString()); 
              //Calculate the time the user should be unlocked
              DateTime unlockDate = lastLockout.AddMinutes(Membership.PasswordAttemptWindow);
  
              //Check to see if it is time to unlock the user
              if (DateTime.Now &gt; unlockDate)
                  Membership.GetUser(Login1.UserName).UnlockUser();
          }
      }
  
  
  }
  
  
  protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
  {
      System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
      TextBox UserName = (TextBox)Login1.FindControl("UserName");
  
      Response.Cookies.Add(new HttpCookie("UserName", Login1.UserName));
      CheckBox RememberMe =  LoginView1.FindControl("RememberMe") as CheckBox;
      //CheckBox RememberMe = (CheckBox).Login1.FindControl("RememberMe"); 
  
      HttpCookie myCookie = new HttpCookie("myCookie");
      if (RememberMe.Checked == true)
      {
          myCookie.Values.Add("username", Login1.UserName);
          myCookie.Expires = DateTime.Now.AddDays(15);
          Response.Cookies.Add(myCookie);
      }
  
  }
  
  
  protected void Login1_LoginError(object sender, EventArgs e)
  {
      System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
      TextBox UserName = (TextBox)Login1.FindControl("UserName");
      Literal FailureText = (Literal)Login1.FindControl("FailureText");
  
  
      //There was a problem logging in the user
      //See if this user exists in the database
  
      MembershipUser userInfo = Membership.GetUser(Login1.UserName);
  
      if (userInfo == null)
      {
          //The user entered an invalid username...
  
          Login1.FailureText = "There is no user in the database with the username " + UserName.Text;
      }
      else
      {
          //See if the user is locked out or not approved
          if (!userInfo.IsApproved)
  
              Login1.FailureText = "When you created your account you were sent an email with steps to verify your account. You must follow these steps before you can log into the site.";
  
          else if (userInfo.IsLockedOut)
  
              Login1.FailureText = "Your account has been temporary locked due to a maximum number of incorrect login attempts.";
  
          else
  
              //The password was incorrect (don't show anything, the Login control already describes the problem)
              Login1.FailureText = string.Empty;
  
      }
  }
  </code></pre>