{"record":{"id":"d617550af6c872a3","repo":"gohugoio/hugo","slug":"cannot-index-slice-array-with-nil","errorCode":null,"errorMessage":"cannot index slice/array with nil","messagePattern":"cannot index slice/array with nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/index.go","lineNumber":88,"sourceCode":"\n\tfor _, i := range indices {\n\t\tindex := reflect.ValueOf(i)\n\t\tvar isNil bool\n\t\tif v, isNil = hreflect.Indirect(v); isNil {\n\t\t\t// See issue 10489\n\t\t\t// This used to be an error.\n\t\t\treturn nil, nil\n\t\t}\n\t\tswitch v.Kind() {\n\t\tcase reflect.Array, reflect.Slice, reflect.String:\n\t\t\tvar x int64\n\t\t\tswitch index.Kind() {\n\t\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\t\t\tx = index.Int()\n\t\t\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\t\t\tx = int64(index.Uint())\n\t\t\tcase reflect.Invalid:\n\t\t\t\treturn nil, errors.New(\"cannot index slice/array with nil\")\n\t\t\tdefault:\n\t\t\t\treturn nil, fmt.Errorf(\"cannot index slice/array with type %s\", index.Type())\n\t\t\t}\n\t\t\tif x < 0 || x >= int64(v.Len()) {\n\t\t\t\t// We deviate from stdlib here.  Don't return an error if the\n\t\t\t\t// index is out of range.\n\t\t\t\treturn nil, nil\n\t\t\t}\n\t\t\tv = v.Index(int(x))\n\t\tcase reflect.Map:\n\t\t\tindex, err := prepareArg(index, v.Type().Key())\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tif x := v.MapIndex(index); x.IsValid() {\n\t\t\t\tv = x\n\t\t\t} else {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/index.go#L70-L106","documentation":"Thrown by the Index/doIndex function when indexing a slice, array, or string with a nil index value (index.go:88). When the index argument's reflect Kind is reflect.Invalid (untyped nil), the indexer cannot resolve an integer position, so it errors. Note: this differs from out-of-range indices, which return nil silently rather than erroring.","triggerScenarios":"Calling {{ index $slice $nilVar }} where $nilVar is unset; {{ index .Params.tags .Params.missing }} where the second arg resolves to nil; multi-level indexing where one level key is nil.","commonSituations":"Optional front matter fields used as indices; template variables never assigned; chained index calls where an intermediate lookup returns nil and is reused as a key.","solutions":["Default the index to a valid value: {{ index $slice ($idx | default 0) }}.","Guard the index with {{ with }}: {{ with $idx }}{{ index $slice . }}{{ end }}.","For string-keyed maps, prefer direct field access or ensure the key is always a string."],"exampleFix":"// before\n{{ index $slice $maybeNilIdx }}\n// after\n{{ with $maybeNilIdx }}{{ index $slice . }}{{ end }}","handlingStrategy":"validation","validationCode":"{{ with $maybeNilIdx }}{{ index $slice . }}{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default index keys: {{ index $slice ($idx | default 0) }}.","Guard index calls with {{ with }} when the key may be nil.","For maps, prefer string keys via default to avoid nil indexing."],"tags":["hugo","template","collections","index","nil-check","argument-validation"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}