{"record":{"id":"5a3aae41b0ce7e4a","repo":"microsoft/aspire","slug":"terminal-host-at-index-i-is-null","errorCode":null,"errorMessage":"Terminal host at index {i} is null.","messagePattern":"Terminal host at index (.+?) is null\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/TerminalAnnotation.cs","lineNumber":82,"sourceCode":"    internal void Initialize(IReadOnlyList<TerminalHostResource> terminalHosts)\n    {\n        ArgumentNullException.ThrowIfNull(terminalHosts);\n\n        if (IsInitialized)\n        {\n            throw new InvalidOperationException(\"TerminalAnnotation has already been initialized.\");\n        }\n\n        if (terminalHosts.Count == 0)\n        {\n            throw new ArgumentException(\"At least one terminal host is required.\", nameof(terminalHosts));\n        }\n\n        for (var i = 0; i < terminalHosts.Count; i++)\n        {\n            if (terminalHosts[i] is null)\n            {\n                throw new ArgumentException($\"Terminal host at index {i} is null.\", nameof(terminalHosts));\n            }\n        }\n\n        _terminalHosts = terminalHosts;\n        IsInitialized = true;\n    }\n}\n\n#pragma warning restore ASPIRETERMINAL001\n\n/// <summary>\n/// Options for configuring a terminal session.\n/// </summary>\n[Experimental(\"ASPIRETERMINAL001\", UrlFormat = \"https://aka.ms/aspire/diagnostics/{0}\")]\npublic sealed class TerminalOptions\n{\n    private int _columns = 120;\n    private int _rows = 30;","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/TerminalAnnotation.cs#L64-L100","documentation":"TerminalAnnotation.Initialize validates each element of the terminalHosts list and throws ArgumentException naming the offending index when a null element is found. This complements the non-null parameter check by catching nulls inside an otherwise non-null collection.","triggerScenarios":"Passing a List<TerminalHostResource?> (or object[] built dynamically) that contains a null element at position i to Initialize — e.g. hosts collected via a loop that appends null on failure, or an array sized but not fully populated.","commonSituations":"Custom tooling that builds the host list with placeholder nulls; refactored code that changed Add logic so some entries never get assigned; deserialized/parsed host lists with missing entries.","solutions":["Filter out nulls before calling Initialize: terminalHosts = allHosts.Where(h => h is not null).ToList().","Fix the producer that appended null so it only adds real TerminalHostResource instances (or throws at the source).","Validate the collection with a null-element check before initialization to fail with a clearer application-level error."],"exampleFix":"// before\nterminalAnnotation.Initialize(hostArray); // hostArray[2] == null\n\n// after\nterminalAnnotation.Initialize(hostArray.Where(h => h is not null).Cast<TerminalHostResource>().ToList());","handlingStrategy":"validation","validationCode":"if (terminalHosts.Any(h => h is null))\n    throw new ArgumentException(\"Terminal host list contains null entries.\");","typeGuard":"static bool HasNoNulls(IReadOnlyList<TerminalHostResource?> hosts) => hosts.All(h => h is not null);","tryCatchPattern":"try\n{\n    terminalAnnotation.Initialize(terminalHosts);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogError(ex, \"Null terminal host entry: {Message}\", ex.Message);\n}","preventionTips":["Build host lists with Add of non-null instances only; never pre-size arrays with null placeholders.","Filter nulls with Where(h => h is not null) before Initialize.","Fail fast at the producer if host creation yields null."],"tags":["argument-validation","null-check","app-model"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}