2

I am getting this error in my custom class. The code is as follows, I highlighted the line where I get there error, and I already checked that the cookie exists:

static private Dictionary<string,string> KeyValueGet()
{
    Dictionary<string, string> ArrKeyVal = new Dictionary<string, string>();
    NameValueCollection CookieData = new NameValueCollection();
    **if (HttpContext.Current.Request.Cookies["CartData"].Values != null)**
    {
        CookieData = HttpContext.Current.Request.Cookies["CartData"].Values;
        string[] CookieKeys = CookieData.AllKeys;
        foreach (string s_key in CookieKeys)
        {
            ArrKeyVal.Add(s_key, CookieData[s_key]);
        }
    }
    return ArrKeyVal;
}

UPDATE: I added an If statement that check's for 'null,' it doesn't even get through this, I get the same exception inside the if statement, it looks like it cannot process what HttpContext.Current.Request is.

Any input is appreciated.

RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53
  • 1
    Did you try checking to see what is null? I.e., is it HttpContext? Current? Request? Cookies["CartData"]? – aquinas May 02 '13 at 02:32
  • 1
    @RealityDysfunction, something is null for the exception to be thrown, it doesn't necessarily have to be `Cookies["CartData"]`, did you debug and see which object is null? It might be `HttpContext.Current.Request`. – Ryan May 02 '13 at 02:37
  • My intuition tells me that HttpContext.Current.Request is the culprit because this is a new class I created (not code-behind) and I was having some trouble earlier getting Request to work until I added HttpContext.Current. – RealityDysfunction May 02 '13 at 02:46
  • what are you keeping in the cookies. And what is `Values` should it be `value` and also I don't think cookie can store anything more than a string. – शेखर May 02 '13 at 02:47
  • "Values" is a specific property of HTTP cookies, it returns a NameValueCollection of key and value pairs. Cookies can store many Key/Value pairs. – RealityDysfunction May 02 '13 at 02:49
  • check for null value use if not null in your code. May be at this point of time the cookie `CartData` is not created or `CartData` is null. – शेखर May 02 '13 at 02:53
  • Sorry, I thought you are at client side. – Xaqron May 02 '13 at 02:53
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 02 '13 at 02:53

1 Answers1

5

The cookie may exist, but to get them you need to have a page, a request from the client and the connection with their browser so to been able to read the cookies, that lives on the browser.

If you make this call from inside a thread, or a process, or on the end of the Page, or anywhere outside the Page, then the HttpContext.Current is null, and/or the HttpContext.Current.Request is null.

Try to run that function from inside your page call.

I usually add an assert for case like that to remind me if I make a call from a thread that did not come from a page.

Debug.Assert(HttpContext.Current != null, "Need to be call from inside a page");
Aristos
  • 66,005
  • 16
  • 114
  • 150