I am getting an 'Object reference not set to an instance of an object' error and i cannot workout why as i am declaring and creating the object globally.
The exception is firing when i try to add the first item to a dictionary. Heres my code:
//declared at the top of my page globally
Dictionary <String, MyCustomType> MyDictionary = new Dictionary();
MyCustomType myCustomType = new MyCustomType();
//Then later in a (private void) method:
//First part of if statement checks if we already have anything in there
//and if the key already exists. If so, it overwrites it
if (MyDictionary != null && MyDictionary.ContainsKey(some_string)
{
// replace the item in the dictionary
}
// if the dictionary is null or the key isnt already in the dictionary.
// This is where it is throwing the exception
else
{
MyDictionary.add(some_string, myCustomType)
}
When debugging through, it does say the MyDictionary is null, but this is to be expected as i have nothing in there yet, and the act off the add in the else statement puts something in there, so im really not sure why its throwing an exception here. The debug also shows that some_string and myCustomType has the values i expect them to have. Can anyone help?