pcbook-go icon indicating copy to clipboard operation
pcbook-go copied to clipboard

panic: runtime error: invalid memory address or nil pointer dereference

Open liuzehao opened this issue 3 years ago • 1 comments

I follow your tutorial completely, but in the [gRPC #10.1] Implement unary gRPC API - Golang,I encountered the following error:

Running tool: /usr/local/opt/go/libexec/bin/go test -timeout 30s -run ^TestServerCreateLaptop$ gitlab.com/techschool/pcbook/service

--- FAIL: TestServerCreateLaptop (0.00s) --- FAIL: TestServerCreateLaptop/success_with_id (0.00s) panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x14bfe44]

goroutine 35 [running]: testing.tRunner.func1.1(0x15663a0, 0x1a9bd50) /usr/local/opt/go/libexec/src/testing/testing.go:940 +0x2f5 testing.tRunner.func1(0xc0000ba000) /usr/local/opt/go/libexec/src/testing/testing.go:943 +0x3f9 panic(0x15663a0, 0x1a9bd50) /usr/local/opt/go/libexec/src/runtime/panic.go:969 +0x166 gitlab.com/techschool/pcbook/service.(*LaptopServer).CreateLaptop(0xc000094f30, 0x16d9a20, 0xc00011e008, 0xc000094f40, 0x160134df, 0xc000046800, 0xc000094f18) /Users/mac/Documents/golang/pcbook/service/laptop_server.go:47 +0x124 gitlab.com/techschool/pcbook/service_test.TestServerCreateLaptop.func1(0xc0000ba000) /Users/mac/Documents/golang/pcbook/service/laptop_server_test.go:72 +0xd0 testing.tRunner(0xc0000ba000, 0xc0000a4ac0) /usr/local/opt/go/libexec/src/testing/testing.go:991 +0xdc created by testing.(*T).Run /usr/local/opt/go/libexec/src/testing/testing.go:1042 +0x357 FAIL gitlab.com/techschool/pcbook/service 0.892s FAIL I don't know how to solve it. Can you help me?

liuzehao avatar Mar 09 '21 14:03 liuzehao

@liuzehao I also encountered the same issue while following [gRPC #10.1] Implement unary gRPC API - Golang,

I solved it by replacing few lines of code,

pb

changing to

image

Reference files, laptop_server.go : https://gist.github.com/yuvan11/ab0f4aea31149f0617847c84796c5e96 laptop_server_test.go : https://gist.github.com/yuvan11/5881101dd93810983f9ac1243f8cbd89

After doing that, test file got passed.

yuvan11 avatar Jul 09 '21 12:07 yuvan11