anon
anon

Reputation:

Can't find the reason for this NullReferenceException

I have the following method:

internal static void removeMethodFromTag(byte p, Method method)
        {
            Console.WriteLine("Remove method " + method.getName() + " from the tag");
            List<Method> outList = new List<Method>();
            methodTaggings.TryGetValue(p, out outList);
            Console.WriteLine(outList.Count);
            outList.Remove(method);
            methodTaggings.Remove(p);
            methodTaggings.Add(p, outList);
        }

This is the dictionarry methodTaggings:

private static Dictionary<byte, List<Method>> methodTaggings = new Dictionary<byte, List<Method>>();

Now I always get a NullReferenceException in the line

Console.WriteLine(outList.Count);

But I can't see why? Even if I don't find a value in the dictionnary, the list shouldn't be null?

Upvotes: 1

Views: 1410

Answers (9)

Homam
Homam

Reputation: 23871

From MSDN:

If the key is not found, then the value parameter gets the appropriate default value for the value type TValue; for example, 0 (zero) for integer types, false for Boolean types, and null for reference types.

And List<Method> is a reference type so it'll return null.

Good luck!

Upvotes: 1

Cheng Chen
Cheng Chen

Reputation: 43531

The value of the list will no longer there, no matter TryGetValue is successful or not, even you have assigned value to it before calling. A simple example:

int num = 9;
int.TryParse("abc", out num);

The TryParse is failed obviously, while the original value 9 will not be kept.

Upvotes: 1

Al Kepp
Al Kepp

Reputation: 5980

Your error is when you call TryGetValue. Parameter outList is marked out, so your original empty list is lost there.

Upvotes: 1

Emond
Emond

Reputation: 50692

TryGetValue is replacing your outList with its default value null

Upvotes: 1

Daniel Gehriger
Daniel Gehriger

Reputation: 7468

There is no need to allocate the outList in your code, as you are passing it to TryGetValue() through its 2nd argument, which is declared as an out parameter.

My guess is that TryGetValue() assigns null to outList.

Upvotes: 1

Sasha Reminnyi
Sasha Reminnyi

Reputation: 3532

It can be initialized to null by the algorithm of TryGetValue. out paramters are always initialized insed method that is called. And your code does not need this line:

List<Method> outList = new List<Method>();

If you use some method that would need ref (not out) parameter, than you would need it. But here out. Refer: http://www.c-sharpcorner.com/UploadFile/mahesh/UsingtheoutParameter12232005041002AM/UsingtheoutParameter.aspx for more details.

Upvotes: 1

Jan Sverre
Jan Sverre

Reputation: 4723

Add an if at the TryGetValue method. I think the TryGetValue is setting the out variable to null when it fails.

internal static void removeMethodFromTag(byte p, Method method)
        {
            Console.WriteLine("Remove method " + method.getName() + " from the tag");
            List<Method> outList = new List<Method>();
            if(methodTaggings.TryGetValue(p, out outList)) {
                Console.WriteLine(outList.Count);
                outList.Remove(method);
                methodTaggings.Remove(p);
                methodTaggings.Add(p, outList);
            }
        }

Upvotes: 2

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60724

The only suspect I can see here is this line:

methodTaggings.TryGetValue(p, out outList);

Could it be that methodTaggings.TryGetValue for the parameter p actually set outList to null?

Place a breakpoint on this line, and I think you will see that after you have stepped over this line, outList will be null.

Upvotes: 1

Paddy
Paddy

Reputation: 33867

Should this not be:

internal static void removeMethodFromTag(byte p, Method method)
        {
            Console.WriteLine("Remove method " + method.getName() + " from the tag");
            List<Method> outList = new List<Method>();
            if (methodTaggings.TryGetValue(p, out outList);
            {
              Console.WriteLine(outList.Count);
              outList.Remove(method);
              methodTaggings.Remove(p);
              methodTaggings.Add(p, outList);
           }
        }

Upvotes: 3

Related Questions