admin
admin copied to clipboard
运行时错误:invalid memory address or nil pointer dereference
这个错误只在windows编译时出现 linux上没问题 错误堆栈:
2017/02/04 22:06:38 http: panic serving 127.0.0.1:5157: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc0423e3400)
C:/Go/src/net/http/server.go:1491 +0x131
panic(0x855660, 0xc04200e130)
C:/Go/src/runtime/panic.go:458 +0x251
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:79 +0x4b
html/template.(*Template).Execute(0x0, 0xb66a00, 0xc04204bad0, 0x8c1c40, 0xc0422c2500, 0x1, 0xc0421f1ce0)
C:/Go/src/html/template/template.go:101 +0x36
github.com/qor/admin.(*Context).Execute(0xc0422c2500, 0x8c8661, 0x9, 0x0, 0x0)
C:/Go/package/src/github.com/qor/admin/context.go:212 +0x5a7
github.com/qor/admin.(*Controller).Dashboard(0xc04233dc10, 0xc0422c2500)
C:/Go/package/src/github.com/qor/admin/controller.go:26 +0x59
github.com/qor/admin.(*Controller).Dashboard-fm(0xc0422c2500)
C:/Go/package/src/github.com/qor/admin/route.go:273 +0x3b
github.com/qor/admin.(*Admin).compile.func2(0xc0422c2500, 0xc04233b3a0)
C:/Go/package/src/github.com/qor/admin/route.go:349 +0x4e7
github.com/qor/admin.Middleware.Next(0x8c91bc, 0xa, 0xc0422d49d0, 0xc04233b3a0, 0xc0422c2500)
C:/Go/package/src/github.com/qor/admin/route.go:38 +0x4f
github.com/qor/admin.(*Admin).compile.func1(0xc0422c2500, 0xc04233b380)
C:/Go/package/src/github.com/qor/admin/route.go:319 +0xbc
github.com/qor/admin.Middleware.Next(0x8d40bc, 0x1c, 0x90b510, 0xc04233b380, 0xc0422c2500)
C:/Go/package/src/github.com/qor/admin/route.go:38 +0x4f
github.com/qor/admin.Admin.registerCompositePrimaryKeyCallback.func1(0xc0422c2500, 0xc0423e9440)
C:/Go/package/src/github.com/qor/admin/composite_primary_key_callback.go:27 +0x1aa
github.com/qor/admin.(*Admin).ServeHTTP(0xc0423ef340, 0xb6a740, 0xc04204bad0, 0xc0420d0c30)
C:/Go/package/src/github.com/qor/admin/route.go:398 +0x39f
net/http.(*ServeMux).ServeHTTP(0xc0423ebda0, 0xb6a740, 0xc04204bad0, 0xc0420d0c30)
C:/Go/src/net/http/server.go:2022 +0x86
net/http.serverHandler.ServeHTTP(0xc0422c2400, 0xb6a740, 0xc04204bad0, 0xc0420d0c30)
C:/Go/src/net/http/server.go:2202 +0x84
net/http.(*conn).serve(0xc0423e3400, 0xb6ad80, 0xc042184080)
C:/Go/src/net/http/server.go:1579 +0x4be
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2293 +0x454
我也遇到一樣的錯誤(我用1.8.1 在 window 底下編的)
@priest671 @bigpigeon 你们的版本是最新的么?看了下 stack 对应的行号,好像没有啥实质的代码?
I wish this was in English, i am having the same error
@samsonpaul did you get the same issue on windows?
and have you updated all qor repos with $GOPATH/github.com/qor/qor/update_all_qor_repos.sh
I am on mac and updating the repos saved the day. I was updating the repos and it kept saying done
without actually updating anything, so I closed that session of terminal and started a new one and it all worked
i am having the same error on mac too
I compiled the code in ubuntu and it's ok. but when i copy the same binary to another ubuntu on server it'm having the same error.
2017/06/11 13:54:47 Finish [GET] /admin Took 0.05ms 2017/06/11 13:54:47 http: panic serving 172.17.0.1:58890: runtime error: invalid memory address or nil pointer dereference goroutine 50 [running]: net/http.(*conn).serve.func1(0xc420d241e0) /usr/local/go/src/net/http/server.go:1721 +0xd0 panic(0x911600, 0xc6c180) /usr/local/go/src/runtime/panic.go:489 +0x2cf html/template.(*Template).escape(0x0, 0x0, 0x0) /usr/local/go/src/html/template/template.go:94 +0x38 html/template.(*Template).Execute(0x0, 0xc40f80, 0xc420d48000, 0x997f80, 0xc420d3e180, 0x1, 0xc420d18b40) /usr/local/go/src/html/template/template.go:117 +0x2f github.com/qor/admin.(*Context).Execute(0xc420d3e180, 0x99f31f, 0x9, 0x0, 0x0) /home/mohammad/gocode/src/github.com/qor/admin/context.go:212 +0x2a8 github.com/qor/admin.(*Controller).Dashboard(0xc420175910, 0xc420d3e180) /home/mohammad/gocode/src/github.com/qor/admin/controller.go:26 +0x52 github.com/qor/admin.(*Controller).Dashboard-fm(0xc420d3e180) /home/mohammad/gocode/src/github.com/qor/admin/route.go:273 +0x34 github.com/qor/admin.(*Admin).compile.func2(0xc420d3e180, 0xc4200bd6e0) /home/mohammad/gocode/src/github.com/qor/admin/route.go:349 +0x4e9 github.com/qor/admin.Middleware.Next(0x9a0182, 0xa, 0xc4201753f0, 0xc4200bd6e0, 0xc420d3e180) /home/mohammad/gocode/src/github.com/qor/admin/route.go:38 +0x48 github.com/qor/admin.(*Admin).compile.func1(0xc420d3e180, 0xc4200bd6c0) /home/mohammad/gocode/src/github.com/qor/admin/route.go:319 +0xb5 github.com/qor/admin.Middleware.Next(0x9ac4ec, 0x1c, 0xa0f0a8, 0xc4200bd6c0, 0xc420d3e180) /home/mohammad/gocode/src/github.com/qor/admin/route.go:38 +0x48 github.com/qor/admin.Admin.registerCompositePrimaryKeyCallback.func1(0xc420d3e180, 0xc42016b280) /home/mohammad/gocode/src/github.com/qor/admin/composite_primary_key_callback.go:27 +0x1a3 github.com/qor/admin.(*Admin).ServeHTTP(0xc4204649a0, 0xc462c0, 0xc420d48000, 0xc4200c8400) /home/mohammad/gocode/src/github.com/qor/admin/route.go:398 +0x319 net/http.(*ServeMux).ServeHTTP(0xc42015a990, 0xc462c0, 0xc420d48000, 0xc4200c8400) /usr/local/go/src/net/http/server.go:2238 +0x130 net/http.serverHandler.ServeHTTP(0xc420465290, 0xc462c0, 0xc420d48000, 0xc4200c8400) /usr/local/go/src/net/http/server.go:2568 +0x92 net/http.(*conn).serve(0xc420d241e0, 0xc46a00, 0xc420d1e580) /usr/local/go/src/net/http/server.go:1825 +0x612 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2668 +0x2ce
@mshafiee request couldn't find the template file, maybe you can try to new a dir in your binary, named app/views/qor, copy github.com/qor/admin/views/* to the new dir.
Same issue in windows.
2017/07/02 20:44:09 http: panic serving [::1]:54793: runtime error: invalid memory address or nil pointer dereference
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc042118000)
D:/Go/src/net/http/server.go:1721 +0xd7
panic(0x8e0c60, 0xbdf2b0)
D:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
D:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0xbb5d40, 0xc04244c380, 0x95db20, 0xc042102930, 0x1, 0xc0423e2960)
D:/Go/src/html/template/template.go:117 +0x36
github.com/qor/admin.(*Context).Execute(0xc042102930, 0x96597e, 0x9, 0x0, 0x0)
C:/Users/premjoy/go/src/github.com/qor/admin/context.go:214 +0x2af
github.com/qor/admin.(*Controller).Dashboard(0xc042106dc0, 0xc042102930)
C:/Users/premjoy/go/src/github.com/qor/admin/controller.go:28 +0x59
github.com/qor/admin.(*Controller).Dashboard-fm(0xc042102930)
C:/Users/premjoy/go/src/github.com/qor/admin/route.go:143 +0x3b
github.com/qor/admin.(*Admin).NewServeMux.func2(0xc042102930, 0xc04244e1c0)
C:/Users/premjoy/go/src/github.com/qor/admin/route.go:176 +0xe7
github.com/qor/admin.Middleware.Next(0x96673c, 0xa, 0xc042106f60, 0xc04244e1c0, 0xc042102930)
C:/Users/premjoy/go/src/github.com/qor/admin/route.go:37 +0x4f
github.com/qor/admin.(*Admin).NewServeMux.func1(0xc042102930, 0xc04244e160)
C:/Users/premjoy/go/src/github.com/qor/admin/route.go:166 +0xbc
github.com/qor/admin.Middleware.Next(0x973e32, 0x1c, 0x9a9138, 0xc04244e160, 0xc042102930)
C:/Users/premjoy/go/src/github.com/qor/admin/route.go:37 +0x4f
github.com/qor/admin.Admin.registerCompositePrimaryKeyCallback.func1(0xc042102930, 0xc04244e660)
C:/Users/premjoy/go/src/github.com/qor/admin/composite_primary_key_callback.go:27 +0x1aa
github.com/qor/admin.(*serveMux).ServeHTTP(0xc04217a208, 0xbba080, 0xc04244c380, 0xc042138000)
C:/Users/premjoy/go/src/github.com/qor/admin/route.go:399 +0x51f
net/http.(*ServeMux).ServeHTTP(0xc04213f3e0, 0xbba080, 0xc04244c380, 0xc042138000)
D:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc042239b80, 0xbba080, 0xc04244c380, 0xc042138000)
D:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc042118000, 0xbba700, 0xc042176680)
D:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
D:/Go/src/net/http/server.go:2668 +0x2d5
Code goes as below:
func main() {
var err error
DB, err := gorm.Open("mysql", fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local", "mysql", "mysql!23", "localhost", "3306", "mediadb"))
if err != nil {
panic(err)
}
fmt.Printf("hello world: %+v\n", DB);
DB.AutoMigrate(&User{})
Admin := admin.New(&qor.Config{DB: DB})
Admin.AddResource(&User{})
mux := http.NewServeMux()
Admin.MountTo("/admin", mux)
fmt.Printf("Admin: %+v\n", Admin);
http.ListenAndServe(":9000", mux)
}
I just try to run the hello world example and try access "http://localhost:9000/admin". I got the same error too...my machine is window 7. The golang is the latest version already.
runtime error: invalid memory address or nil pointer dereference goroutine 66 [running]: net/http.(*conn).serve.func1(0xc042119860) C:/Go/src/net/http/server.go:1697 +0xd7 panic(0x8e4a60, 0xcd4e10) C:/Go/src/runtime/panic.go:491 +0x291 html/template.(*Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x3f html/template.(*Template).Execute(0x0, 0xca3c40, 0xc042378a80, 0x965d80, 0xc04214e540, 0x1, 0xc0423bc150) C:/Go/src/html/template/template.go:119 +0x36 github.com/qor/admin.(*Context).Execute(0xc04214e540, 0x96ce6f, 0x9, 0x0, 0x0) C:/Users/HOMEUSER/go/src/github.com/qor/admin/context.go:227 +0x270 github.com/qor/admin.(*Controller).Dashboard(0xc0421aa860, 0xc04214e540) C:/Users/HOMEUSER/go/src/github.com/qor/admin/controller.go:28 +0x59 github.com/qor/admin.(*Controller).Dashboard-fm(0xc04214e540) C:/Users/HOMEUSER/go/src/github.com/qor/admin/route.go:143 +0x3b github.com/qor/admin.(*Admin).NewServeMux.func2(0xc04214e540, 0xc042141be0) C:/Users/HOMEUSER/go/src/github.com/qor/admin/route.go:176 +0xe7 github.com/qor/admin.Middleware.Next(0x96da2f, 0xa, 0xc0421aa9e0, 0xc042141be0, 0xc04214e540) C:/Users/HOMEUSER/go/src/github.com/qor/admin/route.go:37 +0x4f github.com/qor/admin.(*Admin).NewServeMux.func1(0xc04214e540, 0xc042141b80) C:/Users/HOMEUSER/go/src/github.com/qor/admin/route.go:166 +0xa8 github.com/qor/admin.Middleware.Next(0x9798c2, 0x1c, 0x9b2288, 0xc042141b80, 0xc04214e540) C:/Users/HOMEUSER/go/src/github.com/qor/admin/route.go:37 +0x4f github.com/qor/admin.Admin.registerCompositePrimaryKeyCallback.func1(0xc04214e540, 0xc042140660) C:/Users/HOMEUSER/go/src/github.com/qor/admin/composite_primary_key_callback.go:27 +0x20f github.com/qor/admin.(*serveMux).ServeHTTP(0xc042038150, 0xca8180, 0xc042378a80, 0xc042412600) C:/Users/HOMEUSER/go/src/github.com/qor/admin/route.go:262 +0x599 net/http.(*ServeMux).ServeHTTP(0xc042346fc0, 0xca8180, 0xc042378a80, 0xc042412600) C:/Go/src/net/http/server.go:2254 +0x137 net/http.serverHandler.ServeHTTP(0xc0422401a0, 0xca8180, 0xc042378a80, 0xc042412600) C:/Go/src/net/http/server.go:2619 +0xbb net/http.(*conn).serve(0xc042119860, 0xca8800, 0xc042050180) C:/Go/src/net/http/server.go:1801 +0x724 created by net/http.(*Server).Serve C:/Go/src/net/http/server.go:2720 +0x28f
guys, have any hints on this issue? I can do nothing with the pointer error...
Sorry guys, I just tried method mentioned by @fengde and it works somehow! Thanks very much!
btw, it would be better if you guys can help to make a better handling on the template issues...instead of letting that pointer error thrown...
Related issue when using dep: https://github.com/golang/dep/issues/1840
Same problem with govendor. In govendor and dep package directory(vendor/github.com/qor/admin/
) does not contain views
directory at all. Does not know why ( Workaround with go get -u github.com/qor/admin
Found a solution to the problem for govendor. govendor remove github.com/qor/admin && govendor fetch github.com/qor/admin/^
Hmm, having the same issue using go modules and tutorial code with postgres. go version go1.11.5 windows/amd64, MS Windows 10 Pro.
package main
import ( "fmt" "net/http" // "github.com/qor/qor" "github.com/qor/admin" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" )
// Define a GORM-backend model type User struct { gorm.Model Name string }
// Define another GORM-backend model type Product struct { gorm.Model Name string Description string }
func main() { // Set up the database connectionString := "host=" + "localhost" + " port=" + "5432" + " user=" + "demo" + " dbname=" + "demo" + " password=" + "demo" + " sslmode=" + "disable" db, err := gorm.Open("postgres", connectionString) if err != nil { panic(err) } db.AutoMigrate(&User{}, &Product{})
// Initalize Admin := admin.New(&admin.AdminConfig{DB: db})
// Create resources from GORM-backend model Admin.AddResource(&User{}) Admin.AddResource(&Product{})
// Initalize an HTTP request multiplexer mux := http.NewServeMux()
// Mount admin to the mux Admin.MountTo("/admin", mux)
fmt.Println("Listening on: 9000") http.ListenAndServe(":9000", mux) }
running go run main.go
PS C:\Projects\Programming\go\src\github.com\valasek\qor-admin> go run main.go go: finding github.com/qor/admin latest go: finding github.com/jinzhu/gorm/dialects/postgres latest go: finding github.com/jinzhu/gorm/dialects latest go: finding github.com/qor/session latest go: finding github.com/qor/session/manager latest go: finding github.com/qor/responder latest go: finding github.com/qor/roles latest go: finding github.com/qor/assetfs latest go: finding github.com/lib/pq/hstore latest go: finding github.com/theplant/cldr latest go: finding github.com/qor/qor latest go: finding github.com/qor/qor/resource latest go: finding github.com/qor/qor/utils latest go: finding github.com/jinzhu/inflection latest go: finding github.com/qor/middlewares latest go: finding github.com/qor/validations latest go: finding github.com/jinzhu/now latest go: finding github.com/rainycape/unidecode latest go: finding github.com/asaskevich/govalidator latest Listening on: 9000
Using following modules:
require ( github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf // indirect github.com/gorilla/sessions v1.1.3 // indirect github.com/gosimple/slug v1.4.2 // indirect github.com/jinzhu/gorm v1.9.2 // indirect github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect github.com/lib/pq v1.0.0 // indirect github.com/microcosm-cc/bluemonday v1.0.2 // indirect github.com/qor/admin v0.0.0-20190116035234-0f96b498bae7 // indirect github.com/qor/assetfs v0.0.0-20170713023933-ff57fdc13a14 // indirect github.com/qor/middlewares v0.0.0-20170822143614-781378b69454 // indirect github.com/qor/qor v0.0.0-20181204071447-49b2dd4f5840 // indirect github.com/qor/responder v0.0.0-20171031032654-b6def473574f // indirect github.com/qor/roles v0.0.0-20171127035124-d6375609fe3e // indirect github.com/qor/session v0.0.0-20170907035918-8206b0adab70 // indirect github.com/qor/validations v0.0.0-20171228122639-f364bca61b46 // indirect github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect github.com/theplant/cldr v0.0.0-20170713054817-b97a4c2a76cf // indirect )
and going to address: http://localhost:9000/admin generates:
019/01/31 23:04:31 Finish [GET] /admin Took 0.00ms 2019/01/31 23:04:31 http: panic serving 127.0.0.1:56939: runtime error: invalid memory address or nil pointer dereference goroutine 36 [running]: net/http.(*conn).serve.func1(0xc000052b40) C:/SW/Go/src/net/http/server.go:1746 +0xd7 panic(0x8eba00, 0xd75ec0) C:/SW/Go/src/runtime/panic.go:513 +0x1c7 html/template.(*Template).escape(0x0, 0x0, 0x0) C:/SW/Go/src/html/template/template.go:95 +0x35 html/template.(*Template).Execute(0x0, 0xa2cec0, 0xc000238d20, 0x96bc20, 0xc0000fc0e0, 0x1, 0xc0003f0030) C:/SW/Go/src/html/template/template.go:119 +0x36 github.com/qor/admin.(*Context).Execute(0xc0000fc0e0, 0x973cf6, 0x9, 0x0, 0x0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/context.go:227 +0x24b github.com/qor/admin.(*Controller).Dashboard(0xc000281410, 0xc0000fc0e0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/controller.go:28 +0x4f github.com/qor/admin.(*Controller).Dashboard-fm(0xc0000fc0e0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/route.go:143 +0x3b github.com/qor/admin.(*Admin).NewServeMux.func2(0xc0000fc0e0, 0xc00011d3e0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/route.go:176 +0xe9 github.com/qor/admin.Middleware.Next(0x974990, 0xa, 0xc0002819d0, 0xc00011d3e0, 0xc0000fc0e0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/route.go:37 +0x4f github.com/qor/admin.(*Admin).NewServeMux.func1(0xc0000fc0e0, 0xc00011d3c0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/route.go:166 +0xa8 github.com/qor/admin.Middleware.Next(0x981bcb, 0x1c, 0x9bd4f8, 0xc00011d3c0, 0xc0000fc0e0) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/route.go:37 +0x4f github.com/qor/admin.Admin.registerCompositePrimaryKeyCallback.func1(0xc0000fc0e0, 0xc00011cc60) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/composite_primary_key_callback.go:27 +0x20b github.com/qor/admin.(*serveMux).ServeHTTP(0xc0002500c0, 0xa30380, 0xc000238d20, 0xc000284600) C:/Users/stanislav.valasek/go/pkg/mod/github.com/qor/[email protected]/route.go:267 +0x55d net/http.(*ServeMux).ServeHTTP(0xc000307860, 0xa30380, 0xc000238d20, 0xc000284600) C:/SW/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00032c000, 0xa30380, 0xc000238d20, 0xc000284600) C:/SW/Go/src/net/http/server.go:2741 +0xb2 net/http.(*conn).serve(0xc000052b40, 0xa30640, 0xc00018a180) C:/SW/Go/src/net/http/server.go:1847 +0x64d created by net/http.(*Server).Serve C:/SW/Go/src/net/http/server.go:2851 +0x2fc
I know it's an old thread, but I'm hoping to help someone new to this:
I deploy the admin in kubernetes and got the same error. I resolved it by adding this to the Dockerfile
RUN go get -u github.com/qor/admin
I wrote a quick hack to pull the views I need at run-time once. I was able to get qor
to work with go mod
and go 1.13
. For my needs I .gitignore the generated apps and admin folders, since I only need to use qor
for DB model management. I call generateQorViews
just before web server runs
https://gist.github.com/Krajiyah/ad1de0b61df0e9d19965d264ddf97787
qor admin related views are located in $GOPATH/src/github.com/qor/admin/views/*
you can use it as it is or copy to your environment (for me I add them to the docker image)
and then you need just
adm := admin.New(&admin.AdminConfig{DB: db})
adm.AssetFS.RegisterPath("path_to_the_views")
to say qor where to reach assets :)