panic: runtime error: invalid memory address or nil pointer dereference
Go
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV].
What it means (root cause)
You called a method or accessed a field through a nil pointer/interface — an uninitialized struct pointer, a constructor that returned an error, or an uninitialized map/slice field.
The symptom developers actually see: panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV].
Step-by-step fix
Concrete, ordered steps from the dataset. Apply them in order; each line is a verified action, not generic advice.
- 1Initialize pointers before use: x := &T{} rather than var x *T, and check error returns before dereferencing.
- 2Guard possibly-nil values: if x != nil { x.Field } before accessing.
- 3For maps, use v, ok := m[k]; a nil map read panics on write, so initialize with make(map[...]).
Where this error appears
Languages, frameworks, and runtimes where this error is observed (from the 2026 DevFixPro error dataset, retrieved 2026-08-29):
Go (all versions). The runtime panics immediately; recover() only masks it — fix the nil, don't just recover.
How to prevent it & common questions
Practical prevention plus the questions developers ask most about this error.
What does "panic: runtime error: invalid memory address or nil pointer dereference" mean?
You called a method or accessed a field through a nil pointer/interface — an uninitialized struct pointer, a constructor that returned an error, or an uninitialized map/slice field.
Which environments are affected by panic: runtime error: invalid memory address or nil pointer dereference?
Go
How do I fix panic: runtime error: invalid memory address or nil pointer dereference?
Initialize pointers before use: x := &T{} rather than var x *T, and check error returns before dereferencing. Guard possibly-nil values: if x != nil { x.Field } before accessing. For maps, use v, ok := m[k]; a nil map read panics on write, so initialize with make(map[...]).
How do I prevent panic: runtime error: invalid memory address or nil pointer dereference?
Return errors alongside pointers and check them; initialize maps/slices with make before use.
Should I recover() from the panic?
No. recover() hides the bug; fix the nil dereference at its source. Use nil checks and error returns instead.
Why does writing a nil map panic?
A nil map can be read (returns zero value) but not written. Initialize it with make before storing keys.
Related DevFixPro tools
Real, browser-only utilities on DevFixPro that help while you work through this issue. These are navigation aids, not a substitute for the fix above.
Sources & attribution
- Error records aggregated from Google Search Console query gaps (2026-05-21~2026-08-18) for devfixpro.com, plus official framework docs (MDN, Node.js docs, Python docs, Go.dev, Rust book, Oracle Java docs, Docker docs, npm docs). Source dataset retrieved 2026-08-29. License: CC BY 4.0 — attribute DevFixPro (devfixpro.com).
- Official reference: https://go.dev/tour/methods/10
- All cause, environment, fix and prevention text on this page is taken verbatim from the DevFixPro 2026 error dataset. DevFixPro does not invent root causes or fixes.
How this panic: runtime error: invalid memory address or nil pointer dereference page is built
Each error page is generated from a single record in the DevFixPro 2026 error dataset. The meaning, root cause, environments, fix steps, prevention, and official references are copied verbatim from that dataset and its official-doc sources; related-error links are computed from the error's category and explicit peer list. No root cause or fix is invented. The retrieval date for this dataset is 2026-08-29.