Can't find the reason for this NullReferenceException
I have the follow开发者_高级运维ing 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?
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);
}
}
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);
}
}
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
.
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.
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
.
TryGetValue is replacing your outList with its default value null
Your error is when you call TryGetValue. Parameter outList
is marked out
, so your original empty list is lost there.
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.
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!
精彩评论