runtime error: invalid memory address or nil pointer dereference

来源:5-13 商品管理功能Controller&View开发 (中)

zhangsc

2019-09-14

我按照视频中的教程,可以成功的允许项目,但是在进入 /product/all 的时候会出现运行时错误,是因为我没有查询出来数据导致空指针了吗?

GOROOT=/usr/local/go #gosetup
GOPATH=/Users/zhangshicheng/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/dr/77r7dff174v7rhfy8kz1sbcm0000gn/T/___go_build_main_go /Users/zhangshicheng/go/src/eesion-product/backend/main.go #gosetup
/private/var/folders/dr/77r7dff174v7rhfy8kz1sbcm0000gn/T/___go_build_main_go #gosetup
[DBUG] 2019/09/14 16:23 MVC Controller [controllers.ProductController] [Scope=Stateless]
[DBUG] 2019/09/14 16:23 Dependencies:
        [2] Static binding: services.ProductService{productRepositort:repositores.IProduct(nil)} for field 'ProductService services.ProductService'
[DBUG] 2019/09/14 16:23 GET: /assets/{file:path} -> HandleDir(directory: "./backend/web/assets")
[DBUG] 2019/09/14 16:23 HEAD: /assets/{file:path} -> HandleDir(directory: "./backend/web/assets")
[DBUG] 2019/09/14 16:23 GET: /product/all -> controllers.ProductController.GetAll()
[DBUG] 2019/09/14 16:23 POST: /product/update -> controllers.ProductController.PostUpdate()
[DBUG] 2019/09/14 16:23 Application: 1 registered view engine(s)
[DBUG] 2019/09/14 16:23 Application: running using 1 host(s)
[DBUG] 2019/09/14 16:23 Host: addr is localhost:8080
[DBUG] 2019/09/14 16:23 Host: virtual host is localhost:8080
[DBUG] 2019/09/14 16:23 Host: register startup notifier
[DBUG] 2019/09/14 16:23 Host: register server shutdown on interrupt(CTRL+C/CMD+C)
[DBUG] 2019/09/14 16:23 Host: server will ignore the following errors: [http: Server closed]
Now listening on: http://localhost:8080
Application started. Press CMD+C to shut down.
[HTTP Server] http: panic serving 127.0.0.1:62067: runtime error: invalid memory address or nil pointer dereference
goroutine 56 [running]:
net/http.(*conn).serve.func1(0xc0002d0000)
        /usr/local/go/src/net/http/server.go:1769 +0x139
panic(0x1776860, 0x1f1dbe0)
        /usr/local/go/src/runtime/panic.go:522 +0x1b5
eesion-product/services.(*ProductService).GetAllProduct(...)
        /Users/zhangshicheng/go/src/eesion-product/services/product_service.go:29
eesion-product/backend/web/controllers.(*ProductController).GetAll(0xc0003fe900, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/zhangshicheng/go/src/eesion-product/backend/web/controllers/product_controller.go:18 +0x54
reflect.Value.call(0x1778120, 0xc0003fe900, 0x213, 0x18494b9, 0x4, 0x1f523a0, 0x0, 0x0, 0x1778120, 0x2, ...)
        /usr/local/go/src/reflect/value.go:447 +0x461
reflect.Value.Call(0x1778120, 0xc0003fe900, 0x213, 0x1f523a0, 0x0, 0x0, 0x213, 0x80, 0x203000)
        /usr/local/go/src/reflect/value.go:308 +0xa4
github.com/kataras/iris/mvc.(*ControllerActivator).handlerOf.func2(0x19ab6a0, 0xc00018a240)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/mvc/controller.go:507 +0x697
github.com/kataras/iris/context.Do(0x19ab6a0, 0xc00018a240, 0xc0000bead8, 0x1, 0x1)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/context/context.go:1015 +0x81
github.com/kataras/iris/context.(*context).Do(0xc00018a240, 0xc0000bead8, 0x1, 0x1)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/context/context.go:1202 +0x55
github.com/kataras/iris/core/router.(*routerHandler).HandleRequest(0xc0003fe780, 0x19ab6a0, 0xc00018a240)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/handler.go:251 +0x54f
github.com/kataras/iris/core/router.(*Router).BuildRouter.func1(0x198cde0, 0xc00011c1c0, 0xc000092d00)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/router.go:85 +0x8f
github.com/kataras/iris/core/router.(*Router).ServeHTTP(0xc0001f3480, 0x198cde0, 0xc00011c1c0, 0xc000092d00)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/router.go:162 +0x48
net/http.serverHandler.ServeHTTP(0xc0002428f0, 0x198cde0, 0xc00011c1c0, 0xc000092d00)
        /usr/local/go/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc0002d0000, 0x198f5e0, 0xc0001f3c00)
        /usr/local/go/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2884 +0x2f4
[HTTP Server] http: panic serving 127.0.0.1:62068: runtime error: invalid memory address or nil pointer dereference
goroutine 38 [running]:
net/http.(*conn).serve.func1(0xc0002d2000)
        /usr/local/go/src/net/http/server.go:1769 +0x139
panic(0x1776860, 0x1f1dbe0)
        /usr/local/go/src/runtime/panic.go:522 +0x1b5
eesion-product/services.(*ProductService).GetAllProduct(...)
        /Users/zhangshicheng/go/src/eesion-product/services/product_service.go:29
eesion-product/backend/web/controllers.(*ProductController).GetAll(0xc000410020, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/zhangshicheng/go/src/eesion-product/backend/web/controllers/product_controller.go:18 +0x54
reflect.Value.call(0x1778120, 0xc000410020, 0x213, 0x18494b9, 0x4, 0x1f523a0, 0x0, 0x0, 0x1778120, 0x2, ...)
        /usr/local/go/src/reflect/value.go:447 +0x461
reflect.Value.Call(0x1778120, 0xc000410020, 0x213, 0x1f523a0, 0x0, 0x0, 0x213, 0x0, 0xc0002cfa70)
        /usr/local/go/src/reflect/value.go:308 +0xa4
github.com/kataras/iris/mvc.(*ControllerActivator).handlerOf.func2(0x19ab6a0, 0xc00011e870)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/mvc/controller.go:507 +0x697
github.com/kataras/iris/context.Do(0x19ab6a0, 0xc00011e870, 0xc0000bead8, 0x1, 0x1)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/context/context.go:1015 +0x81
github.com/kataras/iris/context.(*context).Do(0xc00011e870, 0xc0000bead8, 0x1, 0x1)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/context/context.go:1202 +0x55
github.com/kataras/iris/core/router.(*routerHandler).HandleRequest(0xc0003fe780, 0x19ab6a0, 0xc00011e870)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/handler.go:251 +0x54f
github.com/kataras/iris/core/router.(*Router).BuildRouter.func1(0x198cde0, 0xc00011c000, 0xc000092c00)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/router.go:85 +0x8f
github.com/kataras/iris/core/router.(*Router).ServeHTTP(0xc0001f3480, 0x198cde0, 0xc00011c000, 0xc000092c00)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/router.go:162 +0x48
net/http.serverHandler.ServeHTTP(0xc0002428f0, 0x198cde0, 0xc00011c000, 0xc000092c00)
        /usr/local/go/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc0002d2000, 0x198f5e0, 0xc0001f3a00)
        /usr/local/go/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2884 +0x2f4
[HTTP Server] http: panic serving 127.0.0.1:62070: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1(0xc00038a000)
        /usr/local/go/src/net/http/server.go:1769 +0x139
panic(0x1776860, 0x1f1dbe0)
        /usr/local/go/src/runtime/panic.go:522 +0x1b5
eesion-product/services.(*ProductService).GetAllProduct(...)
        /Users/zhangshicheng/go/src/eesion-product/services/product_service.go:29
eesion-product/backend/web/controllers.(*ProductController).GetAll(0xc000410100, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/zhangshicheng/go/src/eesion-product/backend/web/controllers/product_controller.go:18 +0x54
reflect.Value.call(0x1778120, 0xc000410100, 0x213, 0x18494b9, 0x4, 0x1f523a0, 0x0, 0x0, 0x1778120, 0x2, ...)
        /usr/local/go/src/reflect/value.go:447 +0x461
reflect.Value.Call(0x1778120, 0xc000410100, 0x213, 0x1f523a0, 0x0, 0x0, 0x213, 0x320000c0002cfa48, 0x32000000012ad7b1)
        /usr/local/go/src/reflect/value.go:308 +0xa4
github.com/kataras/iris/mvc.(*ControllerActivator).handlerOf.func2(0x19ab6a0, 0xc00011f8c0)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/mvc/controller.go:507 +0x697
github.com/kataras/iris/context.Do(0x19ab6a0, 0xc00011f8c0, 0xc0000bead8, 0x1, 0x1)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/context/context.go:1015 +0x81
github.com/kataras/iris/context.(*context).Do(0xc00011f8c0, 0xc0000bead8, 0x1, 0x1)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/context/context.go:1202 +0x55
github.com/kataras/iris/core/router.(*routerHandler).HandleRequest(0xc0003fe780, 0x19ab6a0, 0xc00011f8c0)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/handler.go:251 +0x54f
github.com/kataras/iris/core/router.(*Router).BuildRouter.func1(0x198cde0, 0xc00011c0e0, 0xc0001f4000)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/router.go:85 +0x8f
github.com/kataras/iris/core/router.(*Router).ServeHTTP(0xc0001f3480, 0x198cde0, 0xc00011c0e0, 0xc0001f4000)
        /Users/zhangshicheng/go/src/github.com/kataras/iris/core/router/router.go:162 +0x48
net/http.serverHandler.ServeHTTP(0xc0002428f0, 0x198cde0, 0xc00011c0e0, 0xc0001f4000)
        /usr/local/go/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc00038a000, 0x198f5e0, 0xc00007a3c0)
        /usr/local/go/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2884 +0x2f4

写回答

1回答

Cap

2019-09-15

加下qq群,找下给老师,老师带你调试

0
0

全流程开发 GO实战电商网站高并发秒杀系统

运用架构设计与系统化思维,从容应对不同流量等级的“秒杀”场景

1462 学习 · 443 问题

查看课程