c# - 找不到这个 NullReferenceException 的原因

标签 c# .net wpf exception

我有以下方法:

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);
        }

这是字典方法标签:

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

现在我总是在行中得到一个NullReferenceException

Console.WriteLine(outList.Count);

但我不明白为什么?即使我在字典中找不到值,列表也不应该为空?

最佳答案

这不应该是:

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);
           }
        }

关于c# - 找不到这个 NullReferenceException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4745387/

相关文章:

c# - 在 Microsoft Word 2010 中将文本发送到邮件合并字段

.net - WPF:MessageBox 是否会破坏 PreviewMouseDown?

WPF 覆盖元数据前台依赖属性

c# - GridView 从抛出异常背后的代码更新

c# - 如何在 GMap.NET 中为路线添加箭头(路线方向)?

c# - File.ReadAllLines() 和 File.ReadAllText() 有什么区别?

.net - 如何在纯 .net 中使 .NET 公共(public)语言运行时 (CLR) 崩溃

c# - 将 DataGrid 绑定(bind)到队列显示为空

c# - 在图像中使用流后如何处理流?

c# - Lucene.NET 2.9 自定义过滤器添加授权