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

2,308 views
Skip to first unread message

RoboTamer

unread,
Sep 23, 2012, 8:08:56 AM9/23/12
to golan...@googlegroups.com
I get this error and can't figure out why.
The error happens in func main.
I have recreated what I am trying to accomplish at golang play and it works fine.
However in my actual code the List struct is in a separate package called jgdb
So the only difference I can see between my actual code, which I have included below and the play code is that I am assigning it  like this: DBM *jgdb.List because it is a separate package.

Somehow var DBM does not retain it's data from one function to the other.
It works fine if I put all the code in to func main, but as soon as I use a second function it gives me this error.
In the code below the value of DBM in main is nil

I been on this for hours now, maybe you can give me a pointer, what am I possibly doing wrong here?

Working example:

The Error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x30 pc=0x400db3]

goroutine 1 [running]:
main.main()
/home/tamer/code/go/src/ctoken/ctoken.go:47 +0x46

goroutine 2 [syscall]:
created by runtime.main
/tmp/bindist927634324/go/src/pkg/runtime/proc.c:221


Code Snip
var (
    DBM *jgdb.List
)

func init() {
    DBM, err := jgdb.NewList("merchants")
    if err != nil {
        println(err.Error())
        os.Exit(2)
    }
    if err := DBM.Load(); err != nil {
        println(err.Error())
        os.Exit(2)
    }
}
func main() {
    fmt.Printf("Data in main: %v\n", DBM.Get(1))
}

Rémy Oudompheng

unread,
Sep 23, 2012, 8:11:04 AM9/23/12
to RoboTamer, golan...@googlegroups.com
On 2012/9/23 RoboTamer <grue...@gmail.com> wrote:
> I get this error and can't figure out why.
> The error happens in func main.
> I have recreated what I am trying to accomplish at golang play and it works
> fine.
> However in my actual code the List struct is in a separate package called
> jgdb
> So the only difference I can see between my actual code, which I have
> included below and the play code is that I am assigning it like this: DBM
> *jgdb.List because it is a separate package.
>
> Somehow var DBM does not retain it's data from one function to the other.
> It works fine if I put all the code in to func main, but as soon as I use a
> second function it gives me this error.
> In the code below the value of DBM in main is nil
>
> I been on this for hours now, maybe you can give me a pointer, what am I
> possibly doing wrong here?
> [snip]
> var (
> DBM *jgdb.List
> )
>
>
> func init() {
> DBM, err := jgdb.NewList("merchants")
> [...]

The ":=" operator declares local variables. So init() is working with
a different variable than your global variable. Use "=" to assign
normally.

Rémy.

RoboTamer

unread,
Sep 23, 2012, 8:14:47 AM9/23/12
to golan...@googlegroups.com, RoboTamer

Oh my god that is it.
Thanks Rémy.
You know I think this sucks.
I been on this for hours!

Johann Höchtl

unread,
Sep 23, 2012, 12:18:55 PM9/23/12
to golan...@googlegroups.com, RoboTamer
One of the rare cases I would want Go to have warnings. Shadowing a global variable with the same name in a function / method is  likely to cause troubles.

BTW you seem to use a quite old version of Go, I think the use of println has been deprecated.

Johann

RoboTamer

unread,
Sep 26, 2012, 9:45:47 PM9/26/12
to golan...@googlegroups.com, RoboTamer

BTW you seem to use a quite old version of Go, I think the use of println has been deprecated.

Johann

Using the latest version and works just fine.
Works also at GoPlay 

Matt Kane's Brain

unread,
Sep 26, 2012, 9:56:00 PM9/26/12
to Johann Höchtl, golan...@googlegroups.com, RoboTamer
On Sun, Sep 23, 2012 at 12:18 PM, Johann Höchtl
<johann....@gmail.com> wrote:
> BTW you seem to use a quite old version of Go, I think the use of println
> has been deprecated.

println has been deprecated since it was created. It is in the spec,
but is not guaranteed to remain.

--
matt kane's brain
http://hydrogenproject.com
Reply all
Reply to author
Forward
0 new messages