Runtime error on request

Hi,

runtime error: invalid memory address or nil pointer dereference
goroutine 17 [running]:
net/http.(*conn).serve.func1(0xc08217c580)
	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
github.com/gorilla/mux.(*Router).ServeHTTP(0xc082008c30, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/goworkspace/src/github.com/gorilla/mux/mux.go:107 +0x28c
github.com/gorilla/csrf.(*csrf).ServeHTTP(0xc082123cc0, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/goworkspace/src/github.com/gorilla/csrf/csrf.go:264 +0x86f
net/http.serverHandler.ServeHTTP(0xc082037f80, 0x1205e38, 0xc0820e2d00, 0xc0821b82a0)
	c:/go/src/net/http/server.go:2081 +0x1a5
net/http.(*conn).serve(0xc08217c580)
	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.

Allright!

in the main-function set the db

var err error
db, err = sql.Open("mysql", "user:password@tcp(xx.xxx.xxx.xxx:3306)/me?charset=utf8")
if err != nil {
	log.Fatal(err)
}

I do not understand properly := ect.

Thx

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.