Why am I getting this NullReferenceException error when testing for null?

I am getting an error on the following line of code: Object reference not set to object instance.

if (Session["AutoCompleteCustomersPhone"] != null)


Complete block of code:

if (Session["AutoCompleteCustomersPhone"] != null)
    earchCustomerPhone = true;
    searchCustomerPhone =


Why, in a test for null, am I getting this error?

enter image description here


source to share

2 answers

Because the sessionn object itself is null

I think you are using webservice which is the reason the session is null *

check this file if you are using session in webservice: Using session state in webservice



Your operator is if

incorrect. You are trying to use a value if it is null.

Change this:

if (Session["AutoCompleteCustomersPhone"] != null)



if (Session["AutoCompleteCustomersPhone"] == null)


I'll fix this first and see if that solves the problem.

The exception coming from that particular line can be caused by:

  • The variable Session

    is null - you can check this in the debugger.
  • The compiled code is different from the source code you use for debugging. Try cleaning and rebuilding.


All Articles