Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
Problem :
You read a file to retrieve some data with io.Reader, then when you attempt to read the same file again. It causes panic with error message :
panic: runtime error: invalid memory address or nil pointer dereference
Looking at the stack trace does not help. What's going on?
Solution :
When a file is read by io.Reader, it usually reads until EOF. So, when you attempt to read from the same file again. It will cause panic... because the pointer is attempting to read pass EOF. To fix this, you will have to reset or rewind the pointer. To do so, use the os.File.Seek()
function to reset.
See how Seek() function is used to reset/rewind the io.Reader example :
package main
import (
"fmt"
"image"
"image/jpeg"
"os"
)
func init() {
// damn important or else At(), Bounds() functions will
// caused memory pointer error!!
image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
}
func main() {
imgfile, err := os.Open("./img.jpg")
if err != nil {
fmt.Println("img.jpg file not found!")
os.Exit(1)
}
defer imgfile.Close()
// get image height and width with image/jpeg
// change accordinly if file is png or gif
imgCfg, _, err := image.DecodeConfig(imgfile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
width := imgCfg.Width
height := imgCfg.Height
fmt.Println("Width : ", width)
fmt.Println("Height : ", height)
// we need to reset the io.Reader again for image.Decode() function below to work
// otherwise we will get -
// panic: runtime error: invalid memory address or nil pointer dereference
// there is no build in rewind, use Seek(0,0)
imgfile.Seek(0, 0)
// get the image
img, _, err := image.Decode(imgfile)
fmt.Println(img.At(10, 10).RGBA())
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, a := img.At(x, y).RGBA()
fmt.Printf("[X : %d Y : %v] R : %v, G : %v, B : %v, A : %v \n", x, y, r, g, b, a)
}
}
}
Reference :
See also : Golang : Get RGBA values of each image pixel
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+22.6k Golang : Test file read write permission example
+11.3k Swift : Convert (cast) Float to String
+17.5k Golang : Iterate linked list example
+20.5k PHP : Convert(cast) int to double/float
+5.1k Unix/Linux : How to archive and compress entire directory ?
+8k Golang : Check if integer is power of four example
+7.1k Golang : alternative to os.Exit() function
+19.6k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+8.7k Golang : Go as a script or running go with shebang/hashbang style
+46k Golang : Encode image to base64 example
+15.6k Golang : Read large file with bufio.Scanner cause token too long error
+16.8k Golang : Find file size(disk usage) with filepath.Walk