Skip to content

Fix: Panic: Runtime error: Invalid memory address or nil pointer dereference

  • by
  • 3 min read

There are more programming languages than ever these days for any beginner programmer to learn. However, as easy as it has become to learn programming, even veteran developers run into random bugs and glitches from time to time.

In this article, we’re talking about the “Panic: Runtime error: Invalid memory address or nil pointer dereference” issue in Golang, its causes and what you can do to fix that problem. 

Also read: How to fix Fatal error: Python.h: No such file or directory?


What causes the Panic: Runtime error: Invalid memory address or nil pointer dereference issue?

The error generally happens when you’re trying to read a nil pointer’s value but it either hasn’t been assigned yet or you’re reading the variable too early in the assignment process. Depending on your exact application of the nil pointer, the steps you need to take to resolve this might change, the but issue remains the same — if you try to read a nil pointer that hasn’t been initialised yet, the program panics and throws this error. 

Top 7 IDEs and Text Editors to code Python, Java, C++, HTML and more

How to fix the Panic: Runtime error: Invalid memory address or nil pointer dereference issue?

To resolve the error, make sure you’re not following any nil pointers. You can do this by implementing conditional checks, such as if-else to ensure that pointers are checked for being nil. If a nil value is not found, the response pointer can be dereferenced and the response can be used or printed, otherwise, your program can print the error and terminate safely. 

Take a look at this snippet for example. 

res, err := client.Do(req)
defer res.Body.Close()

if err != nil {
    return nil, err
}

In this code, the err pointer isn’t nil, but the .Close() method is being accessed before any nil values are checked and hence, the program throws an error. If you replace the aforementioned snippet with the one below, the error will resolve itself as you check for the error immediately. 

res, err := client.Do(req)

if err != nil {
    return nil, err
}
defer res.Body.Close()

Another example can be provided using this snippet:

type Point struct {
	X, Y float64
}

func (p *Point) Abs() float64 {
	return math.Sqrt(p.X*p.X + p.Y*p.Y)
}

func main() {
	var p *Point
	fmt.Println(p.Abs())
}

The aforementioned snippet will give a panic runtime error as the uninitialised pointer in the main function is nil. This can be fixed by either creating a point as follows:

func main() {
	var p *Point = new(Point)
	fmt.Println(p.Abs())
}

Or since methods with pointer receivers take either a value or a pointer, you can just provide the value instead.

func main() {
	var p Point
	fmt.Println(p.Abs())
}

While these might seem like specific examples, as mentioned above, depending on where in your code a nil pointer is initialised, the solution required for you might differ, but the principal remains. 

Also read: Fix: Java: Error: Release version 17 not supported

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>