</bugz loading .
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 !
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.
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
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 :
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.
package main
import "fmt"
var pointer *int // uninitialized pointer, points to nil
func main() {
fmt.Println(*pointer)
}
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.
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.
ptr := someFunction()
fmt.Println(*ptr)
panic: runtime error: invalid memory address or nil pointer dereference
To avoid this, we should confirm that the pointer is not nil before accessing it.
ptr := someFunction()
// Checking if the pointer is nil before dereferencing it
if ptr != nil {
fmt.Println(*ptr)
} else {
fmt.Println("Pointer is nil.")
}
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.
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 ) .