测试断言失败:空引用异常。你调用的对象是空的 [英] Test assert fail: Null reference exception. Object Reference not set to an Instance of an object

查看:314
本文介绍了测试断言失败:空引用异常。你调用的对象是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有code,是造成我悲伤的一条线,但遗憾的是没有它,我的程序中断。这是我能想到的(除了完全调整我的程序)做什么,我想要它做的唯一途径。

此行​​的目的是选择下一个类运行(在菜单),这是其自身的范围之外。麻烦的是,类(登录)的菜单的范围内是否存在。

下面是该行:

  LoginView.Menu.SetMenuView();

任何人都可以建议写这更好的办法?

从技术上讲,没有什么错行了,程序运行顺利。但是当我运行一个测试,测试失败是由于空引用异常。

下面是它出现在类:

控制器:

 公共无效Show_Login(Menu_View MAIN_MENU)
    {
        //创建User_LoginView的对象。
        //设置子窗口的父窗体
        //显示新形式。
        User_LoginView LoginView =新User_LoginView();
        LoginView.MdiParent = MAIN_MENU;        //这行更改提及的菜单变量以备后用。
        LoginView.Menu = MAIN_MENU;
        LoginView.Show();
    }公共静态无效Compare_Login(User_LoginView LoginView)
    {
        //创建User_Model的一个新对象,并从User_Controller填充。
        User_Model AccessModel =新User_Model();
        AccessModel.Name =屏幕名;
        AccessModel.Pwd = screenPwd;        //运行login Comparsion在Database_Facade,并通过在模型中。
        Database_Facade数据库=新Database_Facade();
        Database.GetLoginAccess(AccessModel);
        // screenAccess用于测试。
        screenAccess = AccessModel.AccessLevel;
        Menu_View.accessLevelSet = AccessModel.AccessLevel;        //如果返回放在这里,断言通过,但code其余
        //变得无法访问。
        //返回;        //比较返回ACCESSLEVEL。
        //如果是corect;关闭登录并运行SetMenuView方法,
        //如果它是不正确;示出了一个错误。
        如果(AccessModel.AccessLevel大于0)
        {
            Console.WriteLine(访问级别+ AccessModel.AccessLevel);
            LoginView.Menu.SetMenuView();
            LoginView.Close();
            Menu_View.accessLevelSet = AccessModel.AccessLevel;
        }
        其他
        {
            错误codes_Controller LoginError =新的错误codes_Controller();
            LoginError.WrongLoginError();
        }
    }

查看:

 公共部分类User_LoginView:表单
{
    //这就是新的基准设定,因为当它被调用
    //在登录比较的结束。
    公共Menu_View菜单{搞定;组; }
    公共User_LoginView()
    {
        的InitializeComponent();
    }    私人无效btnLogin_Click(对象发件人,EventArgs的发送)
    {
        User_Controller.Check_Login(本);
    }
}

下面是测试:

  [TestMethod的()]
    公共无效Compare_LoginTest()
    {
        User_LoginView LoginView =新User_LoginView();        User_Controller.screenName =奔;
        User_Controller.screenPwd =密码;
        User_Controller.Compare_Login(LoginView);
        诠释实际= User_Controller.screenAccess;
        INT预期= 1;
        Assert.AreEqual(预期,实际值);
    }


解决方案

在您的测试方法,你是不是设置 LoginView.Menu A值,您在做 Show_Login()

因此​​,当你调用 Compare_Login()从测试,菜单为null。

这可能是值得改变 LoginView 的构造函数接受菜单,并确保测试通过的东西。

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

解决方案

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.

这篇关于测试断言失败:空引用异常。你调用的对象是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆