-1

I have NullReferenceException error in forgotten password section. Where is my mistake and how can I fix it? Here are my codes:

my controller :

     public ActionResult ForGotPasswordSent(ForGotModel forgetModel)
            {
                User user = _userService.GetUserByEmail(forgetModel.Email);
                if (user.IsActive)
                {
                    var objEmail = new EmailHelper();
                    Message msg = new Message();
                    msg.SuccessMessage = "You have sent the mail,Please verify that";
                    msg.IsSuccess = true;
                    string strBody = "Thanks for using the forgot password function.You need to set new password to your account, please click here to proceed";
                    bool result = objEmail.SendMail("noreply@xx.com", "xxtest@gmail.com", "", "Activate Your Account", strBody, true);
                    if (result)
                        return View("Activation", msg);
                    else
                        return View("Activation");
                }
                else
                {

                    this.ModelState.AddModelError("", "You have to activate your account");
                    return View("Activation");
                }
            }

my User class:

  public class User : IPrincipal
    {

        public int Id { get; set; }

        [Required(ErrorMessage = "Kullanıcı adı girilmesi zorunludur.")]
        [Display(Name = "Kullanıcı Adı")]
        public string Username { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        public string Email { get; set; }


        [Required(ErrorMessage = "Password Required")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        public bool IsActive { get; set; }
        public bool IsAdmin { get; set; }
        public bool IsDeleted { get; set; }
        public DateTime? CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }

my ForgotModel

public class ForGotModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

}

_userService.GetUserByEmail method:

 public User GetUserByEmail(string email)
        {
            var user = _userRepository.Get(x => x.Email == email);

            return user;
        }
Kerem Zaman
  • 529
  • 1
  • 5
  • 18

2 Answers2

2

You probably will want to add a null check to user before checking the IsActive property. Try this:

  if (user != null && user.IsActive)
MDiesel
  • 2,647
  • 12
  • 14
  • A NullReferenceException would be thrown in this case if the call to your UserRepository did not return a record matching that email and you then try to access a property (user.IsActive) on that null object. – MDiesel Apr 16 '14 at 19:11
1

GetUserByEmail might return null, when no user is found. Verify that your User object is not null before checking for user.IsActive.

LMS
  • 66
  • 4