anthonypliu
anthonypliu

Reputation: 12437

How do I fix "Object reference not set to an instance of an object"?

I created an mvc4 project in visual studio 2012 RC, and added the ninject.mvc3 package using nuget. It created the standard NinjectWebCommon.cs file and I edited the RegisterServices method like so:

private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IProfileRepository>().To<ProfileRepository>().InSingletonScope(); 
        }    

Here is my interface and profile repository class:

public interface IProfileRepository
    {
        void CreateProfile(UserProfile profile);
    }



public class ProfileRepository : IProfileRepository
    {
        private EFDbContext context = new EFDbContext();

        public void CreateProfile(UserProfile userProfile)
        {
            context.UserProfiles.Add(userProfile);
            context.SaveChanges();
        }

    }

I want to access this my IProfileRepository in my Account Controller like so:

        private readonly IProfileRepository profileRepository;

        public AccountController(IProfileRepository repo){
            profileRepository = repo;
        }
        [AllowAnonymous]
        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    profileRepository.CreateProfile(new UserProfile
                    {
                        UserId = (Guid)Membership.GetUser(HttpContext.User.Identity.Name).ProviderUserKey,
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        School = model.School,
                        Major = model.Major
                    });
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

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

I get an Object reference not set to an instance of an object error when my profileRepostory object is called, hence its probably not being injected. Does anyone know whats wrong? Thanks!

EDIT: Here is my global.asax file:

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

Upvotes: 1

Views: 3722

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

Unless you changed the configuration, Ninject will throw an ActivationException rather than inject null for a dependency. You should check which object is really null.

E.g. Allowing anonymous access is a huge hint that HttpContext.User is what is null.

Upvotes: 4

Related Questions