gohugoio/hugo · error
can't evaluate a nil pointer of type %s by a struct field or
Error message
can't evaluate a nil pointer of type %s by a struct field or map key name %s
What it means
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.
Source
Thrown at tpl/collections/where.go:364
case mt.Type.NumOut() > 2:
return zero, fmt.Errorf("%s is a method of type %s but returns more than 2 outputs", elemName, typ)
case mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType):
return zero, fmt.Errorf("%s is a method of type %s but only returns an error type", elemName, typ)
case mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType):
return zero, fmt.Errorf("%s is a method of type %s returning two values but the second value is not an error type", elemName, typ)
}
res := mt.Func.Call(args)
if len(res) == 2 && !res[1].IsNil() {
return zero, fmt.Errorf("error at calling a method %s of type %s: %s", elemName, typ, res[1].Interface().(error))
}
return res[0], nil
}
// elemName isn't a method so next start to check whether it is
// a struct field or a map value. In both cases, it mustn't be
// a nil value
if isNil {
return zero, fmt.Errorf("can't evaluate a nil pointer of type %s by a struct field or map key name %s", typ, elemName)
}
obj = reflect.Indirect(obj)
switch obj.Kind() {
case reflect.Struct:
ft, ok := obj.Type().FieldByName(elemName)
if ok {
if ft.PkgPath != "" && !ft.Anonymous {
return zero, fmt.Errorf("%s is an unexported field of struct type %s", elemName, typ)
}
return obj.FieldByIndex(ft.Index), nil
}
return zero, fmt.Errorf("%s isn't a field of struct type %s", elemName, typ)
case reflect.Map:
kv := reflect.ValueOf(elemName)
if kv.Type().AssignableTo(obj.Type().Key()) {
return obj.MapIndex(kv), nil
}
return zero, fmt.Errorf("%s isn't a key of map type %s", elemName, typ)View on GitHub (pinned to 52c9bd7908)
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.
Example fix
// before
{{ where $items "Title" "==" "x" }} {{/* some $items entries are nil */}}
// after
{{ $nonNil := where $items "ne" nil }}
{{ where $nonNil "Title" "==" "x" }} Defensive patterns
Strategy: validation
Validate before calling
{{/* Drop nil elements before filtering on a field */}}
{{ $items = where $items "ne" nil }}
{{ where $items "Title" "==" "x" }} Prevention
- 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.
When it happens
Trigger: `{{ where $items "Title" "==" "x" }}` where some elements are nil pointers (`*Item`), or a slice of `*Page` containing nil entries.
Common situations: A collection of pointers with sparse nil entries; optional nested structs (`*Address`) where some are unset; data loaded from JSON with null fields.
Related errors
- %s is an unexported method of type %s
- %s is a method of type %s but requires more than %d paramete
- %s is a method of type %s but returns no output
- %s is a method of type %s but returns more than 2 outputs
- %s is a method of type %s but only returns an error type
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/0a92c88002d74430.
Report an issue: GitHub.