繁体   English   中英

对象引用未设置为对象#2的实例

[英]Object reference not set to an instance of an object #2

public class AccountController : Controller
{

    private LoginModel loginModel = null;

    #region Constructor
    public AccountController(LoginModel loginModel)
    {
        this.loginModel = loginModel;
    }

    public AccountController()
    {
    }
    #endregion

    #region Login
    //
    // GET: /Account/Login
    public ActionResult Login()
    {
        return View();
    }

    //
    // POST: /Account/Login
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(string userName, string password, bool rememberMe, string returnUrl)
    {
        try
        {
            string displayFullName = null, token = null;

            LoginViewModel loginViewModel = new LoginViewModel();
            loginViewModel.UserName = userName;
            loginViewModel.Password = password;
            loginViewModel.RememberMe = rememberMe;

            ModelState.AddModelErrors(loginViewModel.ValidateLogIn());

            if (!ModelState.IsValid)
            {
                return View();
            }

            //login failed than return view. 
            if (!this.loginModel.LogIn(loginViewModel, ref displayFullName, ref token))
            {
                ModelState.AddModelError("_FORM", PortalErrors.IncorrectDataMsg);
                return View();
            }

            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

        }
        catch (Exception exc)
        {
            ModelState.AddModelError("_FORM", PortalErrors.CommonErrMsg);
            return View();
        }
    } 

我在上面的代码中如果(!this.loginModel.LogIn(loginViewModel,ref displayFullName,ref token))出现错误

任何人都可以帮忙找到这个错在哪里吗?

好吧,您有两个this.loginModel其中之一设置this.loginModel (尽管它不会检查是否将其设置为非null值); 另一个没有。

哪个构造函数被调用? 如果它是无参数的,那么您收到该错误并不令我感到惊讶。 您是否需要两个构造函数? 这是由某些DI框架实例化的吗?

当然是因为应该在构造函数中设置loginModel 但是MVC不会触发设置它的构造函数; 它会触发默认的构造函数-到那时它为null; 因此NullReferenceException

我猜你是从其他人那里继承了这个Controller的。 并且他们已经为此编写了单元测试-因此需要额外的构造函数; 除非像Jon提到的那样,否则站点上有一些DI框架处于活动状态(在这种情况下,其配置有误)。

如果有一个DI框架正在创建控制器,则检查它是否可以解析LoginModel的实例。 一种简单且实用的方法是,检查在该构造函数中传递的null LoginModel并引发异常(通常是ArgumentNullException ;使用另一个此处提到的代码约定是一个好计划),然后再次运行该站点。 您可能会收到相同的错误(在这种情况下,可能不涉及DI),或者您将收到ArgumentNullException在这种情况下,并且配置错误。

如果没有,则需要修改控制器的默认构造函数以创建LoginModel的默认实例-假定它的根位于Database或类似的东西中-最终,您需要查看项目的其余代码找出那个。

好吧,看起来this.loginModel为null(或者可能是在this.loginModel.Login调用中)。

我怀疑前者,因为你有

public AccountController() { }

您从未设置过this.loginModel

此外,在此构造函数中

public AccountController(LoginModel loginModel) {
    this.loginModel = loginModel;
}

您没有检查loginModel不为null 我认为您删除了无参数构造函数,应该添加一个合同

Contract.Requires(loginModel != null);

将调试器断点放在有问题的行上。

当代码在此处停止时,是否有任何变量为null?

我的猜测是this.loginModel为null。 您不能在空实例上调用实例方法。

我猜loginModel为null。 如果AccountController是用将导致此问题的无参数构造函数实例化的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM