dotws86260 2017-02-19 11:37
浏览 123
已采纳

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

I'm a nodejs developer and I generally use a structure for my apps which holds a configuration package/object that holds references to my often used libs and configuration options. Generally this configuration object holds my database connections as well and it's reachable trough my application.

I tried to build similar to this in go and failed horribly.

My plan was to build a public variable which holds a reference for my config struct. But when I try to call my Config.Database I get this panic:

2017/02/19 14:05:44 http: panic serving 127.0.0.1:53554: runtime error: invalid memory address or nil pointer dereference
    goroutine 50 [running]:
    net/http.(*conn).serve.func1(0xc42027c000)
        /usr/local/go/src/net/http/server.go:1491 +0x12a
    panic(0x9f45c0, 0xc42000c100)
        /usr/local/go/src/runtime/panic.go:458 +0x243
    main.SignUp(0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
    github.com/kataras/iris.HandlerFunc.Serve(0xafaf00, 0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:211 +0x30
    github.com/kataras/iris.(*Context).Do(0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/context.go:152 +0x4d
    github.com/kataras/iris.(*serveMux).BuildHandler.func1(0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:1059 +0x6ea
    github.com/kataras/iris.(*Framework).Build.func1.1(0xd87e20, 0xc4202701a0, 0xc420284000)
        /home/attila/dev/gopath/src/github.com/kataras/iris/iris.go:411 +0x72
    net/http.HandlerFunc.ServeHTTP(0xc420235080, 0xd87e20, 0xc4202701a0, 0xc420284000)
        /usr/local/go/src/net/http/server.go:1726 +0x44
    net/http.serverHandler.ServeHTTP(0xc420089f80, 0xd87e20, 0xc4202701a0, 0xc420284000)
        /usr/local/go/src/net/http/server.go:2202 +0x7d
    net/http.(*conn).serve(0xc42027c000, 0xd88820, 0xc42015c200)
        /usr/local/go/src/net/http/server.go:1579 +0x4b7
    created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2293 +0x44d
    2017/02/19 14:05:44 http: panic serving 127.0.0.1:53560: runtime error: invalid memory address or nil pointer dereference
    goroutine 51 [running]:
    net/http.(*conn).serve.func1(0xc42027c180)
        /usr/local/go/src/net/http/server.go:1491 +0x12a
    panic(0x9f45c0, 0xc42000c100)
        /usr/local/go/src/runtime/panic.go:458 +0x243
    main.SignUp(0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
    github.com/kataras/iris.HandlerFunc.Serve(0xafaf00, 0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:211 +0x30
    github.com/kataras/iris.(*Context).Do(0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/kataras/iris/context.go:152 +0x4d
    github.com/kataras/iris.(*serveMux).BuildHandler.func1(0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:1059 +0x6ea
    github.com/kataras/iris.(*Framework).Build.func1.1(0xd87e20, 0xc4202a60d0, 0xc4202840f0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/iris.go:411 +0x72
    net/http.HandlerFunc.ServeHTTP(0xc420235080, 0xd87e20, 0xc4202a60d0, 0xc4202840f0)
        /usr/local/go/src/net/http/server.go:1726 +0x44
    net/http.serverHandler.ServeHTTP(0xc420089f80, 0xd87e20, 0xc4202a60d0, 0xc4202840f0)
        /usr/local/go/src/net/http/server.go:2202 +0x7d
    net/http.(*conn).serve(0xc42027c180, 0xd88820, 0xc42015c480)
        /usr/local/go/src/net/http/server.go:1579 +0x4b7
    created by net/http.(*Server).Serve

Here is my configure file. As you can see I'm using tideland/golib/redis for redis connection.

configure.go

package main

import (
    "fmt"
    "strconv"
    "time"

    "github.com/tideland/golib/redis"
)

/*Configuration is the main type of app configuration */
type Configuration struct {
    Database *redis.Connection
}

/*Config is app configuration holder */
var Config *Configuration

/*Configure handles database connection */
func Configure() (*Configuration, error) {

    db, err := redis.Open(redis.TcpConnection("127.0.0.1:6379", 30*time.Second))

    if err != nil {
        fmt.Printf("Database connection error")
        return nil, err
    }
    conn, err := db.Connection()

    n, _ := conn.DoInt("INCR", "IDIDID")

    fmt.Printf(strconv.Itoa(n))

    if err != nil {
        fmt.Printf("Database connection error")
        return nil, err
    }

    /*Config is the main configuration object*/

    Config := &Configuration{conn}

    return Config, err
}

And here is where I use Config.Database.

handlers.go

func SignUp(ctx *iris.Context) {
    mail := ctx.FormValue("email")
    password := ctx.FormValue("password")
    passwordConfirm := ctx.FormValue("password-confirm")

    if password != passwordConfirm {
        ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"passwords dont match"})
    } else {

        user := User{mail, password, 0}
        db := Config.Database

        userID, err := db.DoInt("INCR", "HELLOIRIS:ID")

        if err != nil {
            ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"passwords dont match"})
        } else {

            user.ID = userID

            fmt.Printf("SAVED")

            ctx.Render("signup-success.html", nil)
        }
        ctx.JSON(200, user)
    }
}

After this fail I changed Configure function like this:

configure.go

func Configure() (Config *Configuration, err error) {

    if Config != nil {
    return
    }
}

and I changed the usage at handler

handlers.go

config, err := Configure()

if err != nil {
    ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"try again later"})
    return
}

user := User{mail, password, 0}
db := config.Database

... and everything start to work perfectly.

My problem is I simply don't understand why... Why did I get this panic when I use a public pointer and why didn't I get when I returned the same pointer from a function?

  • 写回答

2条回答 默认 最新

  • doujiao4705 2017-02-19 11:43
    关注
    Config := &Configuration{conn}
    

    This line creates a new local variable Config so the global Config is never set to anything. Drop the : to not create a new variable and use the global Config instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?