{"record":{"id":"52f30afc642c8516","repo":"go-delve/delve","slug":"nil-can-not-be-dereferenced","errorCode":null,"errorMessage":"nil can not be dereferenced","messagePattern":"nil can not be dereferenced","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2313,"sourceCode":"\t\t}\n\t\tfallthrough\n\tdefault:\n\t\tstack.err = fmt.Errorf(\"can not slice %q (type %s)\", astutil.ExprToString(op.Node.X), xev.TypeString())\n\t\treturn\n\t}\n}\n\n// Evaluates a pointer dereference expression: *<subexpr>\nfunc (scope *EvalScope) evalPointerDeref(op *evalop.PointerDeref, stack *evalStack) {\n\txev := stack.pop()\n\n\tif xev.Kind != reflect.Ptr {\n\t\tstack.err = fmt.Errorf(\"expression %q (%s) can not be dereferenced\", astutil.ExprToString(op.Node.X), xev.TypeString())\n\t\treturn\n\t}\n\n\tif xev == nilVariable {\n\t\tstack.err = errors.New(\"nil can not be dereferenced\")\n\t\treturn\n\t}\n\n\tif len(xev.Children) == 1 {\n\t\t// this branch is here to support pointers constructed with typecasts from ints\n\t\txev.Children[0].OnlyAddr = false\n\t\tstack.push(&(xev.Children[0]))\n\t\treturn\n\t}\n\txev.loadPtr()\n\tif xev.Unreadable != nil {\n\t\tval, ok := constant.Uint64Val(xev.Value)\n\t\tif ok && val == 0 {\n\t\t\tstack.err = fmt.Errorf(\"couldn't read pointer: %w\", xev.Unreadable)\n\t\t\treturn\n\t\t}\n\t}\n\trv := &xev.Children[0]","sourceCodeStart":2295,"sourceCodeEnd":2331,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2295-L2331","documentation":"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.","triggerScenarios":"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.","commonSituations":"Inspecting a struct whose pointer field was never initialized; single-stepping before an allocation happened; conditions on breakpoint expressions that dereference possibly-nil pointers.","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."],"exampleFix":"// before\ncondition: \"*ptr == 42\"\n// after\ncondition: \"ptr != nil && *ptr == 42\"","handlingStrategy":"type-guard","validationCode":"pv, err := scope.EvalExpression(ptrExpr, cfg)\nif err != nil { return err }\nif pv.Kind == reflect.Ptr && (pv == proc.NilVariablePlaceholder() || pv.Addr == 0) {\n    return fmt.Errorf(\"%s is nil; not dereferencing\", ptrExpr)\n}","typeGuard":"func dereferenceable(v *proc.Variable) bool {\n    return v != nil && v.Kind == reflect.Ptr && v != nilSentinel\n}","tryCatchPattern":"_, err := scope.EvalExpression(\"*\"+ptrExpr, cfg)\nif err != nil && strings.Contains(err.Error(), \"nil can not be dereferenced\") {\n    return fmt.Errorf(\"%s is nil\", ptrExpr)\n}","preventionTips":["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"],"tags":["go","debugger","nil-pointer","dereference"],"backgroundTag":"nil-pointer-dereference","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}