{"record":{"id":"0a92c88002d74430","repo":"gohugoio/hugo","slug":"can-t-evaluate-a-nil-pointer-of-type-s-by-a-struc","errorCode":null,"errorMessage":"can't evaluate a nil pointer of type %s by a struct field or map key name %s","messagePattern":"can't evaluate a nil pointer of type (.+?) by a struct field or map key name (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/where.go","lineNumber":364,"sourceCode":"\t\tcase mt.Type.NumOut() > 2:\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s but returns more than 2 outputs\", elemName, typ)\n\t\tcase mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType):\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s but only returns an error type\", elemName, typ)\n\t\tcase mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType):\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s returning two values but the second value is not an error type\", elemName, typ)\n\t\t}\n\t\tres := mt.Func.Call(args)\n\t\tif len(res) == 2 && !res[1].IsNil() {\n\t\t\treturn zero, fmt.Errorf(\"error at calling a method %s of type %s: %s\", elemName, typ, res[1].Interface().(error))\n\t\t}\n\t\treturn res[0], nil\n\t}\n\n\t// elemName isn't a method so next start to check whether it is\n\t// a struct field or a map value. In both cases, it mustn't be\n\t// a nil value\n\tif isNil {\n\t\treturn zero, fmt.Errorf(\"can't evaluate a nil pointer of type %s by a struct field or map key name %s\", typ, elemName)\n\t}\n\tobj = reflect.Indirect(obj)\n\tswitch obj.Kind() {\n\tcase reflect.Struct:\n\t\tft, ok := obj.Type().FieldByName(elemName)\n\t\tif ok {\n\t\t\tif ft.PkgPath != \"\" && !ft.Anonymous {\n\t\t\t\treturn zero, fmt.Errorf(\"%s is an unexported field of struct type %s\", elemName, typ)\n\t\t\t}\n\t\t\treturn obj.FieldByIndex(ft.Index), nil\n\t\t}\n\t\treturn zero, fmt.Errorf(\"%s isn't a field of struct type %s\", elemName, typ)\n\tcase reflect.Map:\n\t\tkv := reflect.ValueOf(elemName)\n\t\tif kv.Type().AssignableTo(obj.Type().Key()) {\n\t\t\treturn obj.MapIndex(kv), nil\n\t\t}\n\t\treturn zero, fmt.Errorf(\"%s isn't a key of map type %s\", elemName, typ)","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/where.go#L346-L382","documentation":"Once `evaluateSubElem` (where.go:363-364) confirms the name is not a method, it must read a struct field or map key, both of which require a non-nil target. If `hreflect.Indirect` reports the value is nil, Hugo refuses to dereference it and names the type and the requested field/key.","triggerScenarios":"`{{ where $items \"Title\" \"==\" \"x\" }}` where some elements are nil pointers (`*Item`), or a slice of `*Page` containing nil entries.","commonSituations":"A collection of pointers with sparse nil entries; optional nested structs (`*Address`) where some are unset; data loaded from JSON with null fields.","solutions":["Filter out nil entries before calling `where`: `{{ $items = where $items \"ne\" nil }}` or sanitize in Go.","Ensure the data source never produces nil elements (initialize structs, omit rather than nil).","Use a field on a non-pointer element type, or a path segment that tolerates absence."],"exampleFix":"// before\n{{ where $items \"Title\" \"==\" \"x\" }}  {{/* some $items entries are nil */}}\n// after\n{{ $nonNil := where $items \"ne\" nil }}\n{{ where $nonNil \"Title\" \"==\" \"x\" }}","handlingStrategy":"validation","validationCode":"{{/* Drop nil elements before filtering on a field */}}\n{{ $items = where $items \"ne\" nil }}\n{{ where $items \"Title\" \"==\" \"x\" }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sanitize pointer slices to remove nils before templating.","Prefer value slices or always-initialized pointers in data prep.","Use `with`/`if` guards when dereferencing optional nested structs."],"tags":["hugo","go-templates","where","nil-pointer","reflection"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}