Runtime error on request


runtime error: invalid memory address or nil pointer dereference
goroutine 17 [running]:
	c:/go/src/net/http/server.go:1389 +0xc8
panic(0x8f5520, 0xc0820040d0)
	c:/go/src/runtime/panic.go:443 +0x4f7
database/sql.(*DB).conn(0x0, 0x1, 0x2, 0x0, 0x0)
	c:/go/src/database/sql/sql.go:778 +0xad0
database/sql.(*DB).query(0x0, 0xa0fd60, 0x22, 0xc08202b730, 0x1, 0x1, 0x1, 0xc082032000, 0x0, 0x0)
	c:/go/src/database/sql/sql.go:1073 +0x4d
database/sql.(*DB).Query(0x0, 0xa0fd60, 0x22, 0xc08202b730, 0x1, 0x1, 0x0, 0x0, 0x0)
	c:/go/src/database/sql/sql.go:1061 +0xaa
database/sql.(*DB).QueryRow(0x0, 0xa0fd60, 0x22, 0xc08202b730, 0x1, 0x1, 0x25)
	c:/go/src/database/sql/sql.go:1142 +0x6a
main.reqHandler(0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/goworkspace/src/TaskForce/functions.go:62 +0x382
net/http.HandlerFunc.ServeHTTP(0xa7c620, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/go/src/net/http/server.go:1618 +0x41*Router).ServeHTTP(0xc082008c30, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/goworkspace/src/ +0x28c*csrf).ServeHTTP(0xc082123cc0, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/goworkspace/src/ +0x86f
net/http.serverHandler.ServeHTTP(0xc082037f80, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/go/src/net/http/server.go:2081 +0x1a5
	c:/go/src/net/http/server.go:1472 +0xf35
created by net/http.(*Server).Serve
	c:/go/src/net/http/server.go:2137 +0x455

the error comes when I start a POST request.

Your reqHandler is calling QueryRow on something that is nil and probably shouldn’t be nil.

I set in the main.go the db global

var db *sql.DB

in a other go-file & function grab it this db-variable.

why is nil?

It is perhaps not initialized correctly.


in the main-function set the db

var err error
db, err = sql.Open("mysql", "user:password@tcp(")
if err != nil {

I do not understand properly := ect.


1 Like

That looks like it ought to work.

Can you post the complete source of this file. Thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.