</bugz loading .

Go : runtime error: invalid memory address or nil pointer dereference [solved]

Introduction

panic: runtime error: invalid memory address or nil pointer dereference

This is a fairly common runtime error that occurs mainly in the C and Go languages. In this blog let us walk us through the process of understanding and fixing this error. Let's rock !

Jump to solution 💡

What are nil pointers ?

In simple words, a pointer that does not point to any memory location is called a nil pointer.

  • 👉

    In Golang, nil is used to represent a null ( empty ) value.

  • 👉

    A pointer is a variable that stores the address of another variable in memory.

A pointer becomes a nil pointer when it points to a null value and does not point to the address of any variables.

Cause of runtime error: invalid memory address or nil pointer dereference

As mentioned above, a nil pointer is a pointer that does not point to any memory address.

In Golang, this error generally arise when an attempt to dereference a nil pointer is made.
Attempting to dereference ( access the value being pointed ) a nil pointer will result in this error since the pointer is basically pointing to nothing. The simplest solution for this error is to make sure that the pointer we are trying to derefer is not nil .

If x is nil, an attempt to evaluate *x will cause a run-time panic - Golang docs

Solution and Prevention of runtime error: invalid memory address or nil pointer dereference

In Golang, nil pointers can arise in various situations and can lead to runtime error: invalid memory address or nil pointer dereference if not handled properly.
Here are some of the common causes & solutions to them :

1. Uninitialized Pointer

An Uninitialized pointer is a pointer that is declared but is not initialized ( not given a value ). Basically, when you declare a pointer and do not specify it what to point to, it points nothing ( nil ), hence dereferencing that pointer causes runtime error.

Example

package main

import "fmt"

var pointer *int // uninitialized pointer, points to nil

func main() {
    fmt.Println(*pointer)
}

Output

panic: runtime error: invalid memory address or nil pointer dereference

A simple fix to this is to not leave a pointer uninitialized. Always try to assign a value to the pointer.


2. Functions returning Nil Pointers ( Most common )

Many functions in golang return pointers, some of them might end up being nil due to various reasons. When we try to dereference them, the nil pointer dereference error may occur.

Example

ptr := someFunction()
fmt.Println(*ptr)

Output

panic: runtime error: invalid memory address or nil pointer dereference

Solution

To avoid this, we should confirm that the pointer is not nil before accessing it.

Example

ptr := someFunction()

// Checking if the pointer is nil before dereferencing it
if ptr != nil {
    fmt.Println(*ptr)
} else {
    fmt.Println("Pointer is nil.")
}

Output

Pointer is nil

In the above code, we are confirming that the pointer is not nil, before dereferencing it by using the if statement.

  • 👉

    If the pointer is not nil, we are dereferencing it.

  • 👉

    If the pointer is nil, we are handling it gracefully.

As we are not dereferencing any nil pointers, the panic: runtime error: invalid memory address or nil pointer dereference error will not occur.

Conclusion

In conclusion, handling nil pointers is crucial for preventing panic: runtime error: invalid memory address or nil pointer dereference error. In this blog, we have discussed about various causes of nil pointers and how to avoid them. It is suggested to never leave a pointer uninitialized ( nil ) .