我正在使用 C# 和 .Net 平台编辑一个网页。我有一个注销按钮,当我点击它时会显示错误

I'm working at editing one web page with C# and .Net platform. I have a logout button that when I click it shows me an error

App_Code.p9yip9ku.dll 中发生了“System.NullReferenceException”类型的异常,但未在用户代码中处理

Additional information: Object reference not set to an instance of an object. 这是错误。

我的代码是:

using System.Configuration;
using UserActionLogger;

/// <summary>
/// Summary description for UserActions
/// </summary>
public static class UserData
{
    private static UserActions _uaUserActions;

    public static void InitUserData(int uID, string IP)
    {
        var cString = ConfigurationManager.ConnectionStrings["basteConnectionString"].ConnectionString;
        _uaUserActions = new UserActions(cString, uID, IP);
    }

    public static void Logout()
    {
        _uaUserActions.Logout();
    }

    public static void InsertAction(int actionid, long value)
    {
        _uaUserActions.InsertAction(actionid, value);
    }

}

函数的调用方式如下:

 else if (type == 1002) //User Logout
            {
                Session["current_user"] = "NoUser";

                UserData.Logout();
                var jss = new JavaScriptSerializer();
                this.Response.Write(jss.Serialize(Session["current_user"]));
                this.Response.ContentType = "application/json";
                this.Response.End();
                //TODO : LOGOUT
            }

在你的 UserData 中有 _uaUserActions 当你调用 UserData.logout 时它是 null 你必须在 UserData.Logout();

之前调用 UserData.InitUserData(uID, IP);
else if (type == 1002) //User Logout
        {
            Session["current_user"] = "NoUser";

            UserData.InitUserData(uID,IP); //you have to call this befor Logout method.
            UserData.Logout();
            var jss = new JavaScriptSerializer();
            this.Response.Write(jss.Serialize(Session["current_user"]));
            this.Response.ContentType = "application/json";
            this.Response.End();
            //TODO : LOGOUT
        }

我是这样解决的:

 else if (type == 1002) //User Logout
            {
                UserDTO user = (UserDTO)Session["current_user"];
                int id = user.ID;
                string IPja = this.Request.ServerVariables["REMOTE_ADDR"];
                UserData.InitUserData(id, IPja);
                Session["current_user"] = "NoUser";

                UserData.Logout();
                var jss = new JavaScriptSerializer();
                this.Response.Write(jss.Serialize(Session["current_user"]));
                this.Response.ContentType = "application/json";
                this.Response.End();
                //TODO : LOGOUT
            }