NullReferenceException未被代码处理 [英] NullReferenceException was unhandled by code

查看:70
本文介绍了NullReferenceException未被代码处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



由于某些原因我在代码中更改/删除的内容导致我的登录崩溃并继续抛出NullReferenceException异常。通常我可以找到这种异常的原因,但由于某种原因,这个只是逃避我!



这是我的登录后面的代码:

Hi all,

I am a little bit stuck for some reason something I have changed/deleted in my code has caused my login to crash and keep throwing the NullReferenceException. Normally I can find the cause of this kind of exception but for some reason this one is just escaping me!

This is my code behind on my log in:

protected void Login_Click(object sender, EventArgs e)
    {

        List<User> user1 = new List<User>();

        UserBLL UserDb = new UserBLL();
        User user = new User();
        user.UserName = UserNametxt.Text;
        user.Password = Passwordtxt.Text;
        user1 = UserDb.GetSalt(user);

        string Checkhash = UserDb.CreatePasswordHash(user.Password, user1[0].Salt.ToString());
        string Originalhash = user1[0].Password.ToString();

        if (Checkhash == Originalhash)
        {
            UserBLL UserBLL = new UserBLL();
            List<User> user3 = new List<User>();
            user3 = UserBLL.GetUserID(user);
            user.UserID = Convert.ToInt16(user3[0].UserID.ToString());

            Session["User"] = user.UserName;
            Session["UserID"] = user.UserID;
            Response.Redirect("~/MyProfile.aspx", false);
        }
        else
        {
            Errormsg.Text = "Your UserName or Password is incorrect";
        }

    }



这是发生错误的地方:


This is where the error is occurring:

public List<User> GetSalt(User user)
       {
           return UserDb.GetSalt(user);
       }





这是用户类仅供额外信息:



This is the user class just for extra info:

namespace Entities
{
    public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string Role { get; set; }
        public string Salt { get; set; }
    }
}





如果有人能指出我的错误,我将不胜感激。



谢谢



Dan



If anyone could point out my error I would appreciate it.

Thanks

Dan

推荐答案

很棒(也许不是),问题出在BLL:



Amazingly(maybe not), the problem was in the BLL:

public List<user> GetSalt(User user)
       {
           return UserDb.GetSalt(user);
       }</user>





在班级顶部我将UserDb定义为:





at the top of the class I had defined UserDb as:

UserDAL UserDb;





它想要:





and it wanted:

UserDAL UserDb = new UserDAL();





如果有人能告诉我什么区别是很高兴知道。



谢谢



If anyone could tell me what the difference is it would be nice to know.

Thanks


检查方法GetSalt中的UserDb实例是如何分配的已创建。



Null引用异常的原因是没有为变量UserDb创建实例,并且您试图在GetSalt方法中访问该变量。



值得注意的是,您定义的变量UserDb必须与方法内的UserDb不同。



First所有GetSalt都是UserBLL的一种方法,它再次调用UserDb的另一种方法GetSalt而不是UserBLL。



仔细检查这两个类。或者在这里添加您的代码以获得更好的答案。
Check how the UserDb instance inside the method GetSalt is assigned or created.

The reason for Null Reference exception is no instance created for the variable UserDb and you are trying to access that variable inside the GetSalt method.

It''s worth to note that variable UserDb you have defined must be different from UserDb inside the method.

First of all GetSalt is a method of UserBLL which again calls another method GetSalt of UserDb and not UserBLL.

check these two classes closely. or Add your code here for better answer.


这篇关于NullReferenceException未被代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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