title: Object reference not set to instance of object in mvc[Null Reference Exception]
body: <p>I am creating an MVC application.I am getting an error of Null Reference Exception on change password form functionality.</p>
  <h2>My Model class code</h2>
  <pre><code>public partial class tblUser
  {
      public int Id { get; set; }
  
      [Required(ErrorMessage = &quot;Please Enter Your Email Id&quot;)]
      [EmailAddress(ErrorMessage = &quot;Invalid Email Address&quot;)]
      public string Email { get; set; }
  
      [Required(ErrorMessage = &quot;Please Enter Your Password&quot;)]
      [DataType(DataType.Password)]
      [StringLength(18, ErrorMessage = &quot;The password must be atleast 3 characters long&quot;, MinimumLength = 3)]
      public string Password { get; set; }
  
      [Required(ErrorMessage = &quot;Please Enter Your Password&quot;)]
      [DataType(DataType.Password)]
      [StringLength(18, ErrorMessage = &quot;The password must be atleast 3 characters long&quot;, MinimumLength = 3)]
      public string NewPassword { get; set; }
  }
  </code></pre>
  <h2>My view(Changepassword.cshtml)</h2>
  <pre><code>@model MvcWebApplication1.Models.tblUser
  
  &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;row rowspace1&quot;&gt;
          &lt;div class=&quot;col-sm-12&quot;&gt;
              @Html.ActionLink(&quot;Log Out&quot;, &quot;Login&quot;, &quot;Home&quot;)
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;row&quot;&gt;
          &lt;div id=&quot;CpassTitle&quot;&gt;
              &lt;h3&gt;CHANGE PASSWORD&lt;/h3&gt;
          &lt;/div&gt;
          @using (Html.BeginForm(&quot;Changepassword&quot;, &quot;Home&quot;, FormMethod.Post))
          {
              
                  &lt;table class=&quot;center&quot;&gt;
                      &lt;tr&gt;
                          &lt;td&gt;Old Password&lt;/td&gt;
                          &lt;td&gt;
                              @Html.EditorFor(pass =&gt; pass.Password)
                          &lt;/td&gt;
                          &lt;td&gt;@Html.ValidationMessageFor(pass =&gt; pass.Password)&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr class=&quot;rowspace&quot;&gt;
                          &lt;td&gt;New Password&lt;/td&gt;
  
                          &lt;td&gt;
                              @Html.EditorFor(pass =&gt; pass.NewPassword)
                          &lt;/td&gt;
                          &lt;td&gt;@Html.ValidationMessageFor(pass =&gt; pass.NewPassword)&lt;/td&gt;
                      &lt;/tr&gt;
                    
                      &lt;tr class=&quot;rowspace&quot;&gt;
                          &lt;td colspan=&quot;3&quot; id=&quot;button&quot;&gt;
                              &lt;input type=&quot;submit&quot; value=&quot;Change Password&quot; /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr class=&quot;rowspace&quot;&gt;&lt;td colspan=&quot;3&quot;&gt;@ViewBag.Message&lt;/td&gt;&lt;/tr&gt;
                  &lt;/table&gt;
              
          }
  
      &lt;/div&gt;
  &lt;/div&gt; 
  </code></pre>
  <p>When I click on change password submit button I am getting a <code>NullReferenceException</code> in action method on line <code>userDetail.Password = login.NewPassword;</code></p>
  <pre><code>[HttpPost]
  public ActionResult Changepassword(tblUser login)
  {
      UserDetailsEntities db = new UserDetailsEntities();
      var detail = db.tblUsers.Where(log =&gt; log.Password == login.Password).FirstOrDefault();
      if (detail != null)
      {
          var userDetail = db.tblUsers.FirstOrDefault(c =&gt; c.Email == login.Email);
          userDetail.Password = login.NewPassword;
          
          db.SaveChanges();
          ViewBag.Message = &quot;Record Inserted Successfully!&quot;;
      }
      else
      {
          ViewBag.Message = &quot;Password not Updated!&quot;;
      }
      return View(login);
  }
  </code></pre>