Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an error for Object reference not set to an instance of an object
the code are
C#
public class AccountController : Controller
{
    private IUserProfileService userProfileService;
    private UserManager<applicationuser> UserManager;

    public AccountController(IUserProfileService userProfileService, UserManager<applicationuser> userManager)
    {
        this.userProfileService = userProfileService;
        this.UserManager = userManager;
    }
    public AccountController()
    {

    }
    
    [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }
    
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<actionresult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            
            var user = new ApplicationUser() { UserName = model.UserName };
            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var userId = user.Id;
                var userName = user.UserName;
                userProfileService.CreateUserProfile(userId);
                //await SignInAsync(user, isPersistent: false);

                return RedirectToAction("Index", "Account");
            }
            else
            {
                AddErrors(result);
            }
        }

        return View(model);
    }
Posted
Updated 26-May-15 21:35pm
v2

You need to tell us the description of the issue you are facing. You need to help us to help you out. Following is the reason I could figure out just by reading the code.

The code you gave will throw this error because the default constructor never initializes UserManager object. Also, it is possible that someone can assign null to your parameterized constructor.

I would suggest you to either initialize your UserManager in default constructor as:

C#
public AccountController()
{
  UserManager = new UserManager<applicationuser>();
}


Or add a null check:

C#
if(UserManager !=null)
{
  var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var userId = user.Id;
                var userName = user.UserName;
                userProfileService.CreateUserProfile(userId);
                //await SignInAsync(user, isPersistent: false);

                return RedirectToAction("Index", "Account");
            }
            else
            {
                AddErrors(result);
            }
}


Take the approach which suits your design.
 
Share this answer
 
v3
Comments
C. Nakul 27-May-15 5:26am    
hello Abhipal,


public AccountController(IUserProfileService userProfileService, UserManager<applicationuser> userManager)
{
this.userProfileService = userProfileService;
this.UserManager = userManager;
}



in above code the UserManager in constructor get some value from UserManager but in my code that value are not store in
UserManager variable, so how can i fix that problem.
F-ES Sitecore 27-May-15 5:38am    
Have you used breakpoints and debugging to know that that constructor is being called? Ore are you just assuming it is? If UserManager is null then either that constructor isn't being called, or it is being called bu passed a null userManager param. Either way we have no way of knowing, we can't see the calling code or debug through your app. You need to get familiar with debugging\breakpoints\call stacks etc, we can't debug your processes remotely.
C. Nakul 27-May-15 6:28am    
ya you'r right the constructor can not call, if i post all the code can you help me to debug that code
Abhipal Singh 27-May-15 10:42am    
Possibly the default constructor is being called because of which the value is not being set.
Also, you need to make sure if the parameterize constructor is being called then UserManager should not be passed as null.

Debugging you code by attaching breakpoints is the key to solve your problem.
We can't tell you how to fix this: we don;t have access to your code or data to run it in the same way as you do, so we can't look at what is happening while your code is executing.

So start with the debugger. Whichever method is throwing the exception, put a breakpoint at the top, and step through the code line by line, looking at the values of your variables as you go. For each line, before you execute it look at the code and variables carefully - you are looking for a variable which contains null and has a '.' to its right. When you find it, that's probably where the problem is showing up.

Why it's null is then down to you - generally these problems are as a result of a mistake you made earlier in your code which meant that no value was assigned to something. But we can't find it for you!
 
Share this answer
 
Comments
Abhipal Singh 27-May-15 4:19am    
5ed! Agree

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900