简体   繁体   English

找不到此NullReferenceException的原因

[英]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 现在我总是在该行中得到一个NullReferenceException

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? 即使我在词典中找不到值,列表也不应该为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. 在TryGetValue方法中添加一个if。 I think the TryGetValue is setting the out variable to null when it fails. 我认为TryGetValue在失败时会将out变量设置为null。

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 ? 参数p methodTaggings.TryGetValue可能实际上将outList设置为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 . 在此行上放置一个断点,我想您会看到,越过此行后,outList将为null

It can be initialized to null by the algorithm of TryGetValue . 可以使用TryGetValue算法将其初始化为null out paramters are always initialized insed method that is called. out参数总是被初始化的insed方法被调用。 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. 如果您使用某种需要ref(不是out)参数的方法,那么您将需要它。 But here out. 但是这里。 Refer: http://www.c-sharpcorner.com/UploadFile/mahesh/UsingtheoutParameter12232005041002AM/UsingtheoutParameter.aspx for more details. 有关更多详细信息,请参考: http : //www.c-sharpcorner.com/UploadFile/mahesh/UsingtheoutParameter12232005041002AM/UsingtheoutParameter.aspx

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 . 无需在代码中分配outList ,因为您将其通过其第二个参数传递给TryGetValue() ,该参数被声明为out参数

My guess is that TryGetValue() assigns null to outList . 我的猜测是TryGetValue()null分配给outList

TryGetValue将您的outList替换为其默认值null

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. 无论TryGetValue成功与否,列表的值将不再存在,即使您在调用之前已为其分配了值。 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. TryParse显然失败,而原始值9将不会保留。

Your error is when you call TryGetValue. 您的错误是当您调用TryGetValue时。 Parameter outList is marked out , so your original empty list is lost there. 参数outList被标记为out ,因此原始的空列表在那里丢失。

From MSDN : MSDN

If the key is not found, then the value parameter gets the appropriate default value for the value type TValue; 如果找不到键,则value参数将为值类型TValue获取适当的默认值;否则,值为0。 for example, 0 (zero) for integer types, false for Boolean types, and null for reference types. 例如,对于整数类型,0(零),对于布尔类型,false(假),对于引用类型,null。

And List<Method> is a reference type so it'll return null . List<Method>是引用类型,因此它将返回null

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM