This code throwing out an error:
bool status1 = (bool)Cache["cache_req_head"];
bool status2 = (bool)Cache["cache_super"];
bool status3 = (bool)Cache["cache_head"];
This is how the cache variables were set:
if (checkreqhead == true)
{
Cache["cache_req_head"] = true;
}
else if (checksuper == true)
{
Cache["cache_super"] = true;
}
else if (checkhead == true)
{
Cache["cache_head"] = true;
}
Coming from PHP background, this is awkward. The error is:
Object reference not set to an instance of an object
I’m certain it is something really simple, but probably I can’t spot it.
THANKS ALL FOR HELPING 🙂
“Object reference not set to an instance of an object” is c# lingo for “you did something stupid with a
nullvalue”If the Cache is empty you need to check that first
should be
This is an effect of the fact that value types (like bool, int, etc) in c# can not be null. There is a wrapper,
Nullable<T>with the shorthandT?that you can use if you want to allow null values for the value types.You can cast your value to a
bool?since that allows fornull.You can then check
status1 == nullorstatus1.HasValue, to get the actual bool value you need to pick it out withstatus1.Value. If you pickstatus1.Valuewhilestatus1 == nullyou will get a runtime exception like the one you just got.