Skip to main content
 首页 » 编程设计

c#之如何修复 "Object reference not set to an instance of an object"

2024年02月20日12cloudgamer

我在 visual studio 2012 RC 中创建了一个 mvc4 项目,并使用 nuget 添加了 ninject.mvc3 包。它创建了标准的 NinjectWebCommon.cs 文件,我像这样编辑了 RegisterServices 方法:

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

这是我的界面和配置文件存储库类:

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(); 
        } 
 
    } 

我想像这样在我的帐户 Controller 中访问我的 IProfileRepository:

        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); 
        } 

当我的 profileRepostory 对象被调用时,我得到一个 Object reference not set to an instance of an object 错误,因此它可能没有被注入(inject)。有谁知道怎么了?谢谢!

编辑: 这是我的 global.asax 文件:

public class MvcApplication : System.Web.HttpApplication 
    { 
        protected void Application_Start() 
        { 
            AreaRegistration.RegisterAllAreas(); 
 
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
            RouteConfig.RegisterRoutes(RouteTable.Routes); 
            BundleConfig.RegisterBundles(BundleTable.Bundles); 
        } 
    } 

请您参考如下方法:

除非您更改配置,否则 Ninject 将抛出 ActivationException 而不是为依赖项注入(inject) null。您应该检查哪个对象确实是 null。

例如允许匿名访问是一个巨大的暗示,即 HttpContext.User 是 null。