CJM
CJM

Reputation: 259

Unusual NullReferenceException in mscorlib.dll

I am getting an unusual null reference when I run this code.

Just to clear up, i understand what a null reference is, none of the values that are used in this method are null when it runs. The null reference appears to be somewhere in mscorlib, i have been unable to find anyone reporting a similar issue so far.

Feature is a enum with with 10 or so items in it.

private Dictionary<Feature, bool> dict = new Dictionary<Feature, bool>();

public bool AddFeature(Feature val)
{
        if (!dict.ContainsKey(val))
        {
            dict.Add(val, false);
            return true;
        }
        else
            return false;
}

Feature

[Flags]
public enum Feature
{
    [Description("Other")]
    Other = 0x00000000,

    [Description("Analysis")]
    Analysis = 0x00000001,

    [Description("Campaign")]
    Campaign = 0x00000002,

    [Description("Trends")]
    Trends = 0x00000004,

    [Description("Portal")]
    Portal = 0x00000008,

    [Description("Phone")]
    Phone = 0x00000010,

    [Description("Rents")]
    Rents = 0x00000020,

    [Description("Repairs")]
    Repairs = 0x00000040,

    [Description("Maintenance")]
    Maintenance = 0x00000080,

    [Description("Management")]
    Management = 0x00000100,

    [Description("Services")]
    Services = 0x00000200,

    [Description("All")]
    All = 0x7FFFFFFF
}

The error is:

An exception of type 'System.NullReferenceException' occurred in mscorlib.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
    at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
    at Test.Model.EnabledFeatures.AddFeature(Feature val) in C:\Sandbox\Test\Shared\Model\EnabledFeatures.cs:line 26

I have stepped through my code and the error occurs on the

dict.Add(val, false);

line. When I break at that point, neither dict nor val is null and both have their expected value.

This code used to work, then I was away from it for a few weeks and came back to find it failing. I am wondering if some Windows or Visual Studio updates could have broken this?

Upvotes: 2

Views: 2247

Answers (1)

CJM
CJM

Reputation: 259

Still not sure what the issue was but i have fixed it now by reinstalling .net and many of my visual studio components and packages.

Upvotes: 2

Related Questions