Test assert fail: Null reference exception. Object Reference not set to an Instance of an object

心不动则不痛 提交于 2019-12-13 00:33:59

问题


Ok so I have one line of code that is causing me grief, but unfortunately without it, my program breaks. It is the only way I can think of (aside from completely restructuring my program) to do what I want it to do.

The purpose of this line is to select the next class to run (in the menu), which is outside of its own scope. Trouble is that the class (Login) exists within the scope of the menu.

Here is the line:

LoginView.Menu.SetMenuView();

Can anyone suggest a better way of writing this?

Technically, there is nothing wrong with the line, the program works without a hitch. But when I run a test, the test fails due to the Null Reference Exception.

Here is the classes it appears in:

Controller:

public void Show_Login(Menu_View Main_Menu)
    {
        // Creates an object of the User_LoginView.
        // Set the Parent Form of the Child window
        // Display the new form.
        User_LoginView LoginView = new User_LoginView(); 
        LoginView.MdiParent = Main_Menu;

        // This line changes the reference to the "Menu" variable for later use. 
        LoginView.Menu = Main_Menu;
        LoginView.Show(); 
    }

public static void Compare_Login(User_LoginView LoginView)
    {
        // Creates a new object of User_Model and populates it from the User_Controller.
        User_Model AccessModel = new User_Model();
        AccessModel.Name = screenName;
        AccessModel.Pwd = screenPwd;

        // Runs the Login Comparsion in the Database_Facade, and passes in the Model.
        Database_Facade Database = new Database_Facade();
        Database.GetLoginAccess(AccessModel);
        // screenAccess used for testing.
        screenAccess = AccessModel.AccessLevel;
        Menu_View.accessLevelSet = AccessModel.AccessLevel;

        // If the return is placed here, the assert passes, but the rest of the code 
        // becomes unreachable.
        // return;

        // Compares the returned AccessLevel. 
        // if it is corect; closes the Login and runs the SetMenuView method,
        // if it is incorrect; shows an error.
        if (AccessModel.AccessLevel > 0)
        {
            Console.WriteLine("Access Level " + AccessModel.AccessLevel);
            LoginView.Menu.SetMenuView();
            LoginView.Close();
            Menu_View.accessLevelSet = AccessModel.AccessLevel;
        }
        else
        {
            ErrorCodes_Controller LoginError = new ErrorCodes_Controller();
            LoginError.WrongLoginError();
        } 
    }

View:

public partial class User_LoginView : Form
{
    // This is where the new reference is set, for when it gets called 
    // at the end of the Login Comparison.
    public Menu_View Menu { get; set; }
    public User_LoginView()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        User_Controller.Check_Login(this);
    }
}

Here is the test:

    [TestMethod()]
    public void Compare_LoginTest()
    {
        User_LoginView LoginView = new User_LoginView();

        User_Controller.screenName = "ben";
        User_Controller.screenPwd = "password";
        User_Controller.Compare_Login(LoginView);
        int actual = User_Controller.screenAccess;
        int expected = 1;
        Assert.AreEqual(expected, actual);
    }

回答1:


In your test method, you are not setting a value for LoginView.Menu, which you are doing in Show_Login().

Therefore, when you call Compare_Login() from the test, Menu is null.

It might be worthwhile changing the constructor of LoginView to accept the menu, and ensure that your test passes something in.



来源:https://stackoverflow.com/questions/21224727/test-assert-fail-null-reference-exception-object-reference-not-set-to-an-insta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!