{"record":{"id":"2ba861939a634de2","repo":"iOfficeAI/OfficeCLI","slug":"picture-picindex-not-found-in-sheet-sheetname","errorCode":null,"errorMessage":"Picture[{picIndex}] not found in sheet '{sheetNameFromPath}' (indices are 1-based).","messagePattern":"Picture\\[(.+?)\\] not found in sheet '(.+?)' \\(indices are 1-based\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Query.cs","lineNumber":877,"sourceCode":"            if (spkMatch.Success)\n            {\n                var spkIndex = int.Parse(spkMatch.Groups[1].Value);\n                var spkGroup = GetSparklineGroup(worksheet, spkIndex)\n                    ?? throw new ArgumentException($\"Sparkline[{spkIndex}] not found in sheet '{sheetNameFromPath}'\");\n                return SparklineGroupToNode(sheetNameFromPath, spkGroup, spkIndex);\n            }\n\n            // Handle picture[N] path segment\n            var picMatch = Regex.Match(cellRef, @\"^picture\\[(\\d+)\\]$\", RegexOptions.IgnoreCase);\n            if (picMatch.Success)\n            {\n                var picIndex = int.Parse(picMatch.Groups[1].Value);\n                // GetPictureNode returns null for out-of-range indices (incl.\n                // picture[0]); the bare `!` leaked a NullReferenceException as\n                // an opaque internal_error instead of the not-found message\n                // every sibling element type produces.\n                return GetPictureNode(sheetNameFromPath, worksheet, picIndex, path)\n                    ?? throw new ArgumentException(\n                        $\"Picture[{picIndex}] not found in sheet '{sheetNameFromPath}' (indices are 1-based).\");\n            }\n\n            // Handle shape[N] path segment\n            var shpMatch = Regex.Match(cellRef, @\"^shape\\[(\\d+)\\]$\", RegexOptions.IgnoreCase);\n            if (shpMatch.Success)\n            {\n                var shpIndex = int.Parse(shpMatch.Groups[1].Value);\n                // Same null-leak as picture[N] above.\n                return GetShapeNode(sheetNameFromPath, worksheet, shpIndex, path)\n                    ?? throw new ArgumentException(\n                        $\"Shape[{shpIndex}] not found in sheet '{sheetNameFromPath}' (indices are 1-based).\");\n            }\n\n\n            // If it looks like it could be a malformed cell reference (digits only, etc.), reject it\n            if (Regex.IsMatch(cellRef, @\"^\\d+$\"))\n                throw new ArgumentException($\"Invalid cell reference: '{cellRef}'. Expected format like 'A1', 'B2'.\");","sourceCodeStart":859,"sourceCodeEnd":895,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Query.cs#L859-L895","documentation":"Thrown for /Sheet/picture[N] when GetPictureNode returns null for that 1-based index (out of range, including picture[0]). Previously a bare '!' dereferenced the null and leaked an opaque NullReferenceException; now it surfaces a clear not-found message consistent with sibling element types.","triggerScenarios":"handler.Get(\"/Sheet1/picture[5]\") on a sheet with fewer than 5 pictures. picture[0]. A sheet with no pictures.","commonSituations":"Hard-coded picture index after images were added or removed. Zero-based indexing. Wrong sheet.","solutions":["Use a 1-based index within the picture count.","Confirm pictures exist on the sheet before indexing.","try/catch(ArgumentException) and degrade to 'no picture'."],"exampleFix":"// before\nvar pic = handler.Get(\"/Sheet1/picture[5]\"); // throws if <5 pictures\n\n// after\ntry { var pic = handler.Get(\"/Sheet1/picture[5]\"); }\ncatch (ArgumentException) { /* picture index invalid */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"static int? ElementIndex(string cellRef, string element)\n{\n    var m = Regex.Match(cellRef, $@\"^{Regex.Escape(element)}\\[(\\d+)$\", RegexOptions.IgnoreCase);\n    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;\n}","tryCatchPattern":"try { return handler.Get(\"/Sheet1/picture[5]\"); }\ncatch (ArgumentException) { /* picture index invalid */ return null; }","preventionTips":["Picture indices are 1-based — there is no index 0.","Confirm pictures exist on the sheet before indexing.","Don't reuse a cached index after images are added or removed."],"tags":["excel","picture","path-index","argument-exception"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}