{"record":{"id":"2da660766ed6cd09","repo":"icsharpcode/ILSpy","slug":"types-contains-null-element","errorCode":null,"errorMessage":"types contains null element","messagePattern":"types contains null element","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs","lineNumber":1299,"sourceCode":"\r\n\t\t/// <summary>\r\n\t\t/// Decompile the given types.\r\n\t\t/// </summary>\r\n\t\t/// <remarks>\r\n\t\t/// Unlike Decompile(IMemberDefinition[]), this method will add namespace declarations around the type definitions.\r\n\t\t/// </remarks>\r\n\t\tpublic SyntaxTree DecompileTypes(IEnumerable<TypeDefinitionHandle> types)\r\n\t\t{\r\n\t\t\tif (types == null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(types));\r\n\t\t\tvar decompilationContext = new SimpleTypeResolveContext(typeSystem.MainModule);\r\n\t\t\tsyntaxTree = new SyntaxTree();\r\n\t\t\tvar namespaces = new HashSet<string>();\r\n\t\t\tforeach (var type in types)\r\n\t\t\t{\r\n\t\t\t\tCancellationToken.ThrowIfCancellationRequested();\r\n\t\t\t\tif (type.IsNil)\r\n\t\t\t\t\tthrow new ArgumentException(\"types contains null element\");\r\n\t\t\t\tRequiredNamespaceCollector.CollectNamespaces(type, module, namespaces);\r\n\t\t\t}\r\n\r\n\t\t\tvar decompileRun = CreateDecompileRun(namespaces);\r\n\t\t\tDoDecompileTypes(types, decompileRun, decompilationContext, syntaxTree);\r\n\t\t\tRunTransforms(syntaxTree, decompileRun, decompilationContext);\r\n\t\t\treturn syntaxTree;\r\n\t\t}\r\n\r\n\t\t/// <summary>\r\n\t\t/// Decompile the given types.\r\n\t\t/// </summary>\r\n\t\t/// <remarks>\r\n\t\t/// Unlike Decompile(IMemberDefinition[]), this method will add namespace declarations around the type definitions.\r\n\t\t/// </remarks>\r\n\t\tpublic string DecompileTypesAsString(IEnumerable<TypeDefinitionHandle> types)\r\n\t\t{\r\n\t\t\treturn SyntaxTreeToString(DecompileTypes(types));\r","sourceCodeStart":1281,"sourceCodeEnd":1317,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs#L1281-L1317","documentation":"Thrown by DecompileTypes(IEnumerable<TypeDefinitionHandle>) when the input sequence contains a handle whose IsNil is true. A nil TypeDefinitionHandle is the default/zero value of the metadata-handle struct (token 0), which points at no row in the TypeDef table. The library rejects it because there is no type to decompile, and silently skipping would hide a caller bug.","triggerScenarios":"Calling DecompileTypes with a collection that includes default(TypeDefinitionHandle) or MetadataTokens.TypeDefinitionHandle(0). Usually a LINQ/filter step that produced a nil entry for a missing row, an uninitialized array slot, or an off-by-one handle computation.","commonSituations":"Handles taken from a dictionary/lookup that returns default on miss; arrays allocated larger than the data they hold; translating row indices where a missing row maps to 0.","solutions":["Filter the handle list to exclude nil entries before calling: types.Where(h => !h.IsNil).","When building the list from row indices, skip index 0 and any entry whose GetToken() == 0.","Guard the collection up front: if (types.Any(t => t.IsNil)) throw your own ArgumentException with caller context."],"exampleFix":"// before\nvar handles = metadata.TypeDefinitions.Where(h => Matches(h)).ToList();\nvar tree = decompiler.DecompileTypes(handles);\n\n// after\nvar handles = metadata.TypeDefinitions.Where(h => Matches(h) && !h.IsNil).ToList();\nif (handles.Count == 0) return;\nvar tree = decompiler.DecompileTypes(handles);","handlingStrategy":"validation","validationCode":"var valid = types.Where(h => !h.IsNil).ToList();\nif (valid.Count == 0) return;\nvar tree = decompiler.DecompileTypes(valid);","typeGuard":"static bool IsValidTypeHandle(TypeDefinitionHandle h) => !h.IsNil;","tryCatchPattern":null,"preventionTips":["Never store default(TypeDefinitionHandle) in a list you pass to the decompiler.","Treat metadata token 0 as 'missing' everywhere you build handle collections."],"tags":["decompilation","validation","metadata","argument"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}