首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >恐慌:运行时错误:无效的内存地址或零指针取消引用\ r.body模型

恐慌:运行时错误:无效的内存地址或零指针取消引用\ r.body模型
EN

Stack Overflow用户
提问于 2019-07-21 06:43:21
回答 1查看 1.6K关注 0票数 0

我有问题,新手去编程,如:无效的内存地址或零指针取消引用。

有时候我可以解决这个问题,但这让我很困惑。这是Handler级别的代码,我尝试实现###p.repo.UpdateProfile()和来自r.body解码的数据

代码语言:javascript
复制
//UpdateProfile handler
func (p *Profile) UpdateProfile(w http.ResponseWriter, r *http.Request) {
    var (
        errForm   models.ErrorForm
        resp      models.Response
        respError models.ErrorResponse
        errField  models.ErrField
        data      *models.EditProfile
    )

    userid := r.Context().Value(viper.GetString("token.userid"))
        
        errDecode := json.NewDecoder(r.Body).Decode(&data)

    errPayload := p.repo.UpdateProfile(r.Context(), data, userid)

    if errPayload.Error() == "username_exist" {
        respError.Message = "username already taken"
        respError.Status = 422
        lib.ResJSON(w, respError.Status, respError)
        return
    }
    lib.Catch(errPayload)

    resp.Data = ""
    resp.Message = "Success"
    resp.Status = 200

    lib.ResJSON(w, resp.Status, resp)
}

其方法如下:

代码语言:javascript
复制
func (m *mysqlProfileRepo) UpdateProfile(ctx context.Context, p *models.EditProfile, userid interface{}) error {
    query := ""
    var checkexist int

    row1, err1 := m.Conn.QueryContext(ctx, query, p.Username)
    if err1 != nil {
        return err1
    }

    for row1.Next() {
        if errSc1 := row1.Scan(&checkexist); errSc1 != nil {
            return errors.New("error_scan_db")
        }
    }

    if checkexist != 0 {
        return errors.New("username_exist")
    }

    query1 := ""
    query2 := ""

    stmts := []*lib.PipelineStmt{
        lib.NewPipelineStmt(query1, p.Image, p.Location, p.Link, p.Bio, p.Birthday, userid),
        lib.NewPipelineStmt(query2, p.Username, p.Fullname, userid),
    }

    errTrx := lib.WithTransaction(m.Conn, func(tx lib.Transaction) error {
        _, errRunPipe := lib.RunPipeline(tx, stmts...)
        return errRunPipe
    })

    if errTrx != nil {
        return errTrx
    }

    return nil
}

MyCode在数据库中运行良好,数据成功更新,但响应服务器是

代码语言:javascript
复制
panic: runtime error: invalid memory address or nil pointer dereference
2019/07/21 13:27:36 goroutine 8 [running]:
runtime/debug.Stack(0xc00028e100, 0x16b1a20, 0xc000094040)
    /usr/local/Cellar/go/1.12.6/libexec/src/runtime/debug/stack.go:24 +0x9d
github.com/go-chi/chi/middleware.Recoverer.func1.1(0xc00028e100, 0x16b6be0, 0xc000256280)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/recoverer.go:25 +0x9c
panic(0x155e7a0, 0x1a66a10)
    /usr/local/Cellar/go/1.12.6/libexec/src/runtime/panic.go:522 +0x1b5
audioo_api/handler.(*Profile).UpdateProfile(0xc000020030, 0x16b6be0, 0xc000256280, 0xc00028e200)
    /Users/rizaldinurmuhammad/go/src/audioo_api/handler/profile.go:73 +0x524
net/http.HandlerFunc.ServeHTTP(0xc000020050, 0x16b6be0, 0xc000256280, 0xc00028e200)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
audioo_api/lib/mid.Authenticate.func1.1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/audioo_api/lib/mid/auth.go:48 +0x71e
net/http.HandlerFunc.ServeHTTP(0xc00000e0a0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*ChainHandler).ServeHTTP(0xc000066140, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/chain.go:31 +0x52
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a5f80, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc000020040, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a5f80, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:70 +0x451
github.com/go-chi/chi.(*Mux).Mount.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:292 +0x127
net/http.HandlerFunc.ServeHTTP(0xc00000e0e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a59e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc0001db880, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a59e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:70 +0x451
github.com/go-chi/chi.(*Mux).Mount.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:292 +0x127
net/http.HandlerFunc.ServeHTTP(0xc00000e180, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a5920, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc000020090, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
audioo_api/lib/mid.Limit.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/audioo_api/lib/mid/limit.go:79 +0xe5
net/http.HandlerFunc.ServeHTTP(0xc00000e1a0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/cors.(*Cors).Handler.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/cors/cors.go:201 +0x1a4
net/http.HandlerFunc.ServeHTTP(0xc00000e1c0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.Recoverer.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/recoverer.go:35 +0x9f
net/http.HandlerFunc.ServeHTTP(0xc00000e1e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.RedirectSlashes.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/strip.go:53 +0x202
net/http.HandlerFunc.ServeHTTP(0xc00000e200, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.(*Compressor).Handler.func1.1(0x1f69180, 0xc000256240, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/compress.go:190 +0x254
net/http.HandlerFunc.ServeHTTP(0xc00000e2c0, 0x1f69180, 0xc000256240, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.RequestLogger.func1.1(0x16b78a0, 0xc000226000, 0xc00028e000)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/logger.go:46 +0x291
net/http.HandlerFunc.ServeHTTP(0xc00009a210, 0x16b78a0, 0xc000226000, 0xc00028e000)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a5920, 0x16b78a0, 0xc000226000, 0xc000154000)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:82 +0x294
net/http.serverHandler.ServeHTTP(0xc00009c410, 0x16b78a0, 0xc000226000, 0xc000154000)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc0002ce640, 0x16b8aa0, 0xc000067340)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:2884 +0x2f4

我试图更改上下文以实现数据库,但响应仍然相同。

我想知道这与r.body有关的问题,但我不知道如何解决

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-21 09:11:38

根据Body io.ReadCloser的包文档:

对于客户端请求,零主体意味着请求没有主体,例如GET请求。对于服务器请求,请求主体总是非零。

您说,它是一个服务器,所以,它总是非零的,因此错误在Decode调用中,您将一个指针传递给这个零指针。

datanil指针,&data是指向nil指针的指针。

如果数据是非接口类型,则解决方案,除非models包显式禁止new(EditProfile)

代码语言:javascript
复制
    data = new(models.EditProfile)
    errDecode := json.NewDecoder(r.Body).Decode(data)

如果数据是一个接口,那么首先为它分配一个实际值,然后再次传递而不引用DecodeerrDecode := json.NewDecoder(r.Body).Decode(data)

在后一种情况下,还需要反复检查是否声明了正确的*models.EditProfile类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57130942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档