角色登录错误System.NullReferenceException:“对象引用未设置为对象的实例。”

时间:2019-06-27 07:09:13

标签: c# logging

我正在尝试对三种类型的角色进行登录身份验证,以管理员或学生的身份登录其工作,但是当我以教师身份登录时,其给出错误“ System.NullReferenceException:'对象引用未设置为的实例一个对象。'“
请查看代码,并给我一些好的解决方案。...

 public bool LoggingIn(ref bool logged, out bool startup)
    {
        string pswd;
        Console.Write("Write your Email :");
        email = Console.ReadLine();
        Console.Write("Write your Password :");
        pswd =Console.ReadLine();

        var adminContext = new UMSDbContext();
        var tchremail = (adminContext.Teachers.Where(t => t.Email == email).FirstOrDefault());
        var stdemail = (adminContext.Students.Where(s => s.Email == email).FirstOrDefault());
        var data = (adminContext.Admins.Where(u =>u.Email == email && u.Password==pswd).FirstOrDefault());
        if (data != null && email == "rana@gmail.com")
        {
            Console.WriteLine("Login Succesully");
            Console.WriteLine();
            logged = true;
            startup = false;
            RunAdmin();
            return logged;
        }
        else if (data != null && email == stdemail.Email)
        {
            Console.WriteLine("Login Succesully");
            Console.WriteLine();
            logged = true;
            startup = false;
            RunStudent();
            return logged;
        }
        else if (data != null && email == tchremail.Email)
        {
            Console.WriteLine("Login Succesully");
            Console.WriteLine();
            logged = true;
            startup = false;
            RunTeacher();
            return logged;
        }
        else
        {
            Console.WriteLine(" Invalid Email or Password");
            Console.WriteLine();
            startup = true;
            return logged;
        }
    }

0 个答案:

没有答案