go-delve/delve · error
nil can not be dereferenced
Error message
nil can not be dereferenced
What it means
evalPointerDeref evaluates *p. A nil pointer expression is represented by the nilVariable sentinel, which has no memory to dereference, so delve throws this instead of attempting a read. This is the debugger-side equivalent of Go's nil dereference, raised before any memory access.
Source
Thrown at pkg/proc/eval.go:2313
}
fallthrough
default:
stack.err = fmt.Errorf("can not slice %q (type %s)", astutil.ExprToString(op.Node.X), xev.TypeString())
return
}
}
// Evaluates a pointer dereference expression: *<subexpr>
func (scope *EvalScope) evalPointerDeref(op *evalop.PointerDeref, stack *evalStack) {
xev := stack.pop()
if xev.Kind != reflect.Ptr {
stack.err = fmt.Errorf("expression %q (%s) can not be dereferenced", astutil.ExprToString(op.Node.X), xev.TypeString())
return
}
if xev == nilVariable {
stack.err = errors.New("nil can not be dereferenced")
return
}
if len(xev.Children) == 1 {
// this branch is here to support pointers constructed with typecasts from ints
xev.Children[0].OnlyAddr = false
stack.push(&(xev.Children[0]))
return
}
xev.loadPtr()
if xev.Unreadable != nil {
val, ok := constant.Uint64Val(xev.Value)
if ok && val == 0 {
stack.err = fmt.Errorf("couldn't read pointer: %w", xev.Unreadable)
return
}
}
rv := &xev.Children[0]View on GitHub (pinned to a23773e6c3)
Solutions
- Check the pointer before dereferencing: print p != nil, then print *p only when non-nil.
- Use Go-style optional access in expressions where supported, or guard breakpoint conditions with `p != nil && *p == x`.
- Step to a point after initialization; the nil may be valid at the current stop point.
- In tooling, detect Kind == reflect.Ptr with Addr/children indicating nil and render '<nil>' instead of issuing the deref.
Example fix
// before condition: "*ptr == 42" // after condition: "ptr != nil && *ptr == 42"
Defensive patterns
Strategy: type-guard
Validate before calling
pv, err := scope.EvalExpression(ptrExpr, cfg)
if err != nil { return err }
if pv.Kind == reflect.Ptr && (pv == proc.NilVariablePlaceholder() || pv.Addr == 0) {
return fmt.Errorf("%s is nil; not dereferencing", ptrExpr)
} Type guard
func dereferenceable(v *proc.Variable) bool {
return v != nil && v.Kind == reflect.Ptr && v != nilSentinel
} Try / catch
_, err := scope.EvalExpression("*"+ptrExpr, cfg)
if err != nil && strings.Contains(err.Error(), "nil can not be dereferenced") {
return fmt.Errorf("%s is nil", ptrExpr)
} Prevention
- Guard breakpoint conditions with ptr != nil before dereferencing
- Check pointer fields for nil before expanding them in UIs
- Step past initialization before inspecting pointees
- Render nil pointers as '<nil>' instead of issuing deref requests
When it happens
Trigger: Evaluating *p where p == nil (e.g., *nil or *typedNil); dereferencing a pointer-typed expression that evaluated to the nil constant; print *s.PointerField when PointerField is nil.
Common situations: Inspecting a struct whose pointer field was never initialized; single-stepping before an allocation happened; conditions on breakpoint expressions that dereference possibly-nil pointers.
Related errors
- nil pointer dereference
- expression %q (%s) does not support indexing
- could not decode first frame
- unable to find function context
- unable to find locals: no debug information present in binar
AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31).
Data as JSON: /api/errors/52f30afc642c8516.
Report an issue: GitHub.