紧急情况:运行时错误:无效的内存地址或nil指针取消引用|| 人体模型

詹姆斯·里扎尔迪(James Rizaldi):

我在使用新手Go编程时遇到问题,例如:无效的内存地址或nil指针取消引用

有时我可以解决问题,但这使我感到困惑。这是处理程序级别的代码,我尝试实现### p.repo.UpdateProfile()和r.body解码中的数据

//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)
}

和方法类似:


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在数据库中运行良好,数据已成功更新,但是响应服务器是

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有关,但我不知道该如何解决

丹尼斯·沃林(Denis Volin):

根据Body io.ReadCloser的软件包文档:

对于客户端请求,nil主体表示该请求没有主体,例如GET请求。对于服务器请求,请求正文始终为非零。

您说的是服务器,因此它始终为非零,因此错误发生在Decode调用中,您将指针传递给nil指针。

data是一个nil指针,&data是一个指针的nil指针。

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

    data = new(models.EditProfile)
    errDecode := json.NewDecoder(r.Body).Decode(data)

如果data是接口,请先为其分配一个实值,然后再次传递而无需引用DecodeerrDecode := json.NewDecoder(r.Body).Decode(data)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

运行时错误:POST请求的内存地址无效或nil指针取消引用(golang-gorm)

紧急情况:运行时错误:无效的内存地址或具有更大数据的nil指针取消引用

单元测试代码:=运行时错误:无效的内存地址或nil指针取消引用

运行时错误:无效的内存地址或nil指针取消引用(2)

错误:紧急:运行时错误:无效的内存地址或nil指针取消引用。在另一个结构中存在的结构中更改映射,

在主块中捕获到错误后,程序会出现紧急情况。紧急:运行时错误:无效的内存地址或nil指针取消引用

运行时错误:再次出现无效的内存地址或nil指针取消引用

运行时错误:查询时无效的内存地址或nil指针取消引用

为什么在新计算机上访问Firestore时会出现“紧急:运行时错误:无效的内存地址或nil指针取消引用”?

如何使用gorm解决“运行时错误:无效的内存地址或nil指针取消引用”

紧急服务[:: 1]:46738:运行时错误:无效的内存地址或nil指针取消引用

运行时错误:无效的内存地址或公共指针中的nil指针取消引用

紧急情况:运行时错误:在拆分应用程序时无效的内存地址或nil指针取消引用

Golang恐慌:运行时错误:无效的内存地址或nil指针取消引用

为什么gorm db.First()会因“无效的内存地址或取消引用nil指针”而出现紧急情况?

使用xlsx软件包出现紧急情况:运行时错误:无效的内存地址或nil指针取消引用Go

紧急:运行时错误:无效的内存地址或nil指针取消引用

运行时错误:无效的内存地址或nil指针取消引用

Golang,goroutines:紧急情况:运行时错误:无效的内存地址

Golang:恐慌:运行时错误:无效的内存地址或nil指针取消引用

运行时错误:带有模板的martini中无效的内存地址或nil指针取消引用

执行-运行时错误:无效的内存地址或nil指针取消引用

执行:紧急:运行时错误:无效的内存地址或nil指针取消引用

紧急情况:运行时错误:运行Blockchainr时无效的内存地址或nil指针取消引用

golang:运行时错误:无效的内存地址或nil指针取消引用

转至:XORKeyStream():运行时错误:无效的内存地址或nil指针取消引用

Hyperledger Fabric:紧急情况:运行时错误:在fabric / peer / common.NewPeerClientForAddress处无效的内存地址或nil指针取消引用

运行时错误:无效的内存地址或分页时取消了nil指针的引用

接口返回运行时错误:无效的内存地址或 nil 指针取消引用