YRK
(Yash)
October 31, 2021, 11:50am
1
http: panic serving [::1]:60288: runtime error: invalid memory address or nil pointer dereference
goroutine 21 [running]:
net/http.(*conn).serve.func1()
/usr/local/go/src/net/http/server.go:1801 +0xb9
panic({0x12d5ae0, 0x158de40})
/usr/local/go/src/runtime/panic.go:1047 +0x266
html/template.(*Template).escape(0x0)
/usr/local/go/src/html/template/template.go:97 +0x34
html/template.(*Template).Execute(0x0, {0x1388be0, 0xc00017c0e0}, {0x0, 0x0})
/usr/local/go/src/html/template/template.go:121 +0x32
github.com/YRK/jewellery-store/pkg/render.RenderTemplate ({0x138f170, 0xc00017c0e0}, {0x131fe4c, 0x0})
/Users/yash_khandelwal943/Desktop/Development/go-code/jewellery-store/pkg/render/render.go:12 +0x9f
github.com/YRK/jewellery-store/pkg/handlers.Home ({0x138f170, 0xc00017c0e0}, 0x0)
/Users/yash_khandelwal943/Desktop/Development/go-code/jewellery-store/pkg/handlers/handlers.go:11 +0x2f
net/http.HandlerFunc.ServeHTTP(0x0, {0x138f170, 0xc00017c0e0}, 0x0)
/usr/local/go/src/net/http/server.go:2046 +0x2f
net/http.(*ServeMux).ServeHTTP(0x0, {0x138f170, 0xc00017c0e0}, 0xc000190200)
/usr/local/go/src/net/http/server.go:2424 +0x149
net/http.serverHandler.ServeHTTP({0xc00011d2c0}, {0x138f170, 0xc00017c0e0}, 0xc000190200)
/usr/local/go/src/net/http/server.go:2878 +0x43b
net/http.(*conn).serve(0xc000138fa0, {0x13903e0, 0xc00011d020})
/usr/local/go/src/net/http/server.go:1929 +0xb08
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:3033 +0x4e8
Please help me with this !
skillian
(Sean Killian)
October 31, 2021, 11:58am
2
Without the code, we can only only guess at what the problem could be. I tried going to https://github.com/YRK/jewellery-store/ to check the code, but I got a 404. Maybe it’s a private repo or not yet pushed, etc., so I couldn’t find the code myself. With an error like, “invalid memory address or nil pointer dereference,” it sounds like something , somewhere in your program is nil
. Based on the stack trace, e.g.:
/usr/local/go/src/html/template/template.go:97 +0x34
html/template.(*Template).Execute(0x0, {0x1388be0, 0xc00017c0e0}, {0x0, 0x0})
That first 0x0 after Execute(
leads me to believe your template is nil
. I can’t know why without seeing your code. Are you handling errors?
2 Likes
YRK
(Yash)
October 31, 2021, 2:09pm
3
Hi Sean, Thank you for your response,
This piece of code is for rendering :::
func RenderTemplate(rw http.ResponseWriter, tmpl string) {
parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl)
err := parsedTemplate.Execute(rw, nil)
if err != nil {
fmt.Println(“error parsing template:”, err)
return
}
}
in my main.go
func main() {
http.HandleFunc("/", handlers.Home)
http.HandleFunc("/about", handlers.About)
fmt.Println(fmt.Sprintf(“Starting application on port %s”, portNumber))
err := http.ListenAndServe(portNumber, nil)
if err != nil {
log.Fatal(err)
}
}
skillian
(Sean Killian)
October 31, 2021, 2:54pm
4
You’re ignoring any errors that might occur when parsing the files. If this function fails, it will return a nil
template and a non-nil
error
. You should always check all error
return values unless the documentation says that the error
will be nil
(the only ones I know of off the top of my head are the (*strings.Builder).Write*
methods.
YRK
(Yash)
October 31, 2021, 4:54pm
5
Thanks Sean, The issue was with template file, it was unable to parse it. It’s been resolved now
Hey Yash
Can you tell me how you fixed it. I am following the same course.
YRK
(Yash)
January 7, 2022, 5:23pm
7
Hi Jitendra,
Kindly do the error handling in this case.
parsedTemplate, err := template.ParseFiles("./templates/" + tmpl)
if err != nil {
log.Fatal(“Unable to parse from template:”, err)
}
system
(system)
Closed
April 7, 2022, 5:23pm
8
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.