'I am getting error while passing model object to repository method(_accountObj )

I am trying to calling repository method with the help of Interface in controller because I am using dependency injection, but at this point _accountObj.SaveAdmin(_accountModel). I am getting error like this Object reference not set to an instance of an object - "_accountObj was null". I am not able to save user signup data into repository to database. Please help me to resolve my issue. I will also upload image of the code and error for your better understanding.

using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Inspinia_MVC5.Models;
using Domain.Models;
using Services.Interface;

namespace Inspinia_MVC5.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private ApplicationSignInManager _signInManager;
        private ApplicationUserManager _userManager;
        private readonly IAccount _accountObj;

        public AccountController()
        {
        }

        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAccount account)
        {
            UserManager = userManager;
            SignInManager = signInManager;
            _accountObj = account;
        }

        public ApplicationSignInManager SignInManager
        {
            get
            {
                return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
            }
            private set
            {
                _signInManager = value;
            }
        }

        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }

        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    AccountModel _accountModel = new AccountModel();

                    _accountModel.Id = user.Id;
                    _accountModel.Email = user.Email;
                    _accountModel.EmailConfirmed = user.EmailConfirmed;
                    _accountModel.PasswordHash = user.PasswordHash;
                    _accountModel.SecurityStamp = user.SecurityStamp;
                    _accountModel.PhoneNumber = user.PhoneNumber;
                    _accountModel.PhoneNumberConfirmed = user.PhoneNumberConfirmed;
                    _accountModel.TwoFactorEnabled = user.TwoFactorEnabled;
                    _accountModel.LockoutEndDateUtc = user.LockoutEndDateUtc;
                    _accountModel.LockoutEnabled = user.LockoutEnabled;
                    _accountModel.AccessFailedCount = user.AccessFailedCount;
                    _accountModel.UserName = user.Id;

                    if (_accountModel != null)
                    {
                        //Getting error here at "_accountObj"
                        await _accountObj.SaveAdmin(_accountModel);
                    }

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
    }
}

Error image, please see the image also:

Image 1

Image 2



Solution 1:[1]

DLDR; remove the constructor with 0 parameters.

You have 2 constructors. Put a breakpoint in both, and debug your code to find out which one is being called. I'm guessing it's the default constructor (the one with 0 parameters), and therefore UserManager, SignInManager and _accountObj are all null.

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IAccount account)
    {
        UserManager = userManager;
        SignInManager = signInManager;
        _accountObj = account;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neil