简体   繁体   中英

ASP.NET C# System.NullReferenceException on requesting cookies

I have a problem when I request cookies, but I don't understand what I can do to solve it.

My code:

public partial class Admin_LogIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["IvoucherCookieAdminPassword"] != null && Request.Cookies["IvoucherCookieAdminMail"] != null)
            if ("***".Equals(Request.Cookies["IvoucherCookieAdminPassword"].Value.ToString()) && "***".Equals(Request.Cookies["IvoucherCookieAdminMail"].Value.ToString()))
                Response.Redirect("Benvenuto.aspx");
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        if (UserTextBox.Text == "***" && PasswordTextBox.Text == "***")
        {
            Response.Cookies["IvoucherCookieAdminMail"].Value = UserTextBox.Text;
            Response.Cookies["IvoucherAdminCookiePassword"].Value = PasswordTextBox.Text;
            Response.Cookies["IvoucherCookieAdminMail"].Expires = DateTime.Now.AddHours(2);
            Response.Cookies["IvoucherAdminCookiePassword"].Expires = DateTime.Now.AddHours(2);
            Response.AddHeader("REFRESH", "0.1;Benvenuto.aspx");
        }
        else
        {
            GeneralErrorTextBox.Text = "Nome utente e/o password errata!";
        }
    }
}

So I have tried this to fix it:

public partial class Admin_LogIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var adminuser = Request.Cookies["IvoucherCookieAdminMail"];
        var adminpassword = Request.Cookies["IvoucherAdminCookiePassword"];
        if (adminuser != null && adminpassword != null)
            if ("***".Equals(Request.Cookies["IvoucherCookieAdminPassword"].Value.ToString()) && "***".Equals(Request.Cookies["IvoucherCookieAdminMail"].Value.ToString()))
                Response.Redirect("Benvenuto.aspx");
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        if (UserTextBox.Text == "***" && PasswordTextBox.Text == "***")
        {
            Response.Cookies["IvoucherCookieAdminMail"].Value = UserTextBox.Text;
            Response.Cookies["IvoucherAdminCookiePassword"].Value = PasswordTextBox.Text;
            Response.Cookies["IvoucherCookieAdminMail"].Expires = DateTime.Now.AddHours(2);
            Response.Cookies["IvoucherAdminCookiePassword"].Expires = DateTime.Now.AddHours(2);
            Response.AddHeader("REFRESH", "0.1;Benvenuto.aspx");
        }
        else
        {
            GeneralErrorTextBox.Text = "Nome utente e/o password errata!";
        }
    }
}

But I always get the following error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

 [NullReferenceException: Object reference not set to an instance of an object.] Admin_LogIn.Page_Load(Object sender, EventArgs e) +126 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1016

How can I solve it?

This error happens when you try to use a property or call a method of an object that is null. More details:

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Might be wrong but it looks like you've wrongly named one of your cookie items. You call it:

var adminpassword = Request.Cookies["IvoucherAdminCookiePassword"];

But on the next line, you reference the IvoucherCookieAdminPassword .

So, is it CookieAdminPassword or AdminCookiePassword or both?

This is because you are not setting the value of

Request.Cookies["IvoucherCookieAdminPassword"].Value **and** Request.Cookies["IvoucherCookieAdminMail"].Value

while you are making check on them in Page_Load function when they contain null,before make a condition you have to set their value.

to view more deatils on .Equal() please see,
http://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx Read more about HttpRequest.Cookies Property go to ....
http://msdn.microsoft.com/en-us/library/system.web.httprequest.cookies(v=vs.110).aspx

Happy Coding.......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM