Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:03:30+00:00 2026-05-30T16:03:30+00:00

public class AccountController : Controller { private LoginModel loginModel = null; #region Constructor public

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

I get the error on my above code on line if (!this.loginModel.LogIn(loginViewModel, ref displayFullName, ref token))

Can any one help with this to find what’s wrong in this one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:03:31+00:00Added an answer on May 30, 2026 at 4:03 pm

    Surely it’s because the loginModel is supposed to be set in the constructor; but MVC will not fire the constructor that sets it; it’ll be firing the default constructor – so by that point it’s null; hence the NullReferenceException.

    I’m going to take a guess that you’ve inherited this Controller from someone else; and that they have written unit tests for it – hence the additional constructor; unless, as Jon has mentioned, there is some DI framework active on the site (in which case it’s misconfigured).

    If there’s a DI framework creating the controller – then check that it can resolve an instance of LoginModel. An easy way, and good practise, is to check for a null LoginModel being passed in that constructor and throw an exception (typically ArgumentNullException; the use of Code Contracts as mentioned by another answer here is a good plan) and then run the site again. Either you’ll get the same error (in which case there’s probably no DI involved), or you’ll get your ArgumentNullException – in which case there is and it’s badly configured.

    If there isn’t, you need to modify the default constructor of the controller to create the default instance of LoginModel – presumably it has roots in a Database or something like that – ultimately you’ll need to look at the rest of the project’s code to figure that one out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I write a controller like below: public class AccountController : Controller { public ActionResult
In my AccountController class I have the following: public ActionResult Verification(string userGuid) { Debug.WriteLine(userGuid);
Public Class FooBar { private int _foo; public int Foo { get { return
public class Address { public string ZipCode {get; set;} } public class Customer {
Folks, I am MVC 2 newbie and stuck on this problem: AccountModuls.cs public class
I've something like <?php class AccountController extends Zend_Controller_Action { public function init() { if(!Zend_Auth::getInstance()->hasIdentity())
Public Class frmMain Private p_dlgAdd As frmAdd = Nothing Public ReadOnly Property _dlgAdd As
public class PhotoList : ObservableCollection<ImageFile> { public PhotoList() { } **//this is the line
public class Bird { private static int id = 0; private String kind; public
Entity: @Entity @Table(name=user_account) public class UserAccount implements Serializable{ private static final long serialVersionUID =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.