I am struggling to find a solution to this problem. I see lots of simlilar entries on this site that touch upon this subject, but I can't seem to arrive at a solution. I am trying to check a table in cache to see if it already exists and if not, populate it. Below is my code for that check and it errors on the 'if' statement telling me 'System.NullReferenceException: Object reference not set to an instance of an object'. This is puzzling because shouldn't the '.IsNullOrEmpty' catch this? I figure if the 1st element in the array is null or empty then it hasn't been cached yet and therefore take action.
string[] saveCatList = Cache["Categories" + Session["sessopnID"]] as string[];
if (string.IsNullOrEmpty(saveCatList[0]))
{
WBDEMOReference.getcatlist_itemcategories[] categories;
strResult = callWebServ.getcatlist(Session["sessionID"].ToString(),
out strResultText, out dNumOfCat, out categories);
for (int i = 0; i < categories.Length; i++)
{
//ddCat is the ID number of the category drop down list
ddCat.Items.Add(new ListItem(categories[i].categorydesc.ToString(),
categories[i].categorynumber.ToString()));
}
}