Reciever nil problem

I must have misunderstood something, when I write like this:

_, err = m.ToolbarRefresh.Connect("clicked", m.FundList.updateFundsValue)

where updateFundsValue() is a function declared on the fundList type :

func (f *fundList) updateFundsValue() {
 go func() {
         ...
 }()
}

I get an error at runtime :

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

because the receiver f is nil inside updateFundsValue(). But when I write it like this it works:

_, err = m.ToolbarRefresh.Connect("clicked", func() {
	m.FundList.updateFundsValue()
})

m.FundList has a value in this case, and the reciever f has a value. Why is that? The first way of doing it works in other cases, like :

_, err = m.ToolbarQuit.Connect("clicked", m.shutDown) 

Ps. this code is using GoTK3 (https://github.com/gotk3/gotk3), but I think the question is not connected to GoTK3 since it works fine in the second case. Can you not use the one-liner code when you have a type m which has a pointer to another type FundList? That seems random…

Syntactically, there’s no reason it shouldn’t work. Are you checking the error value after the call to Connect? Are m and m.FundList non-nil at the time you click?

That is good to know, and I have never had a problem with this before, so I did not expect any problem here…but there was…

Yes, and there are no errors…

They should be, m is a pointer to the main form, and that form is still open when the button is clicked, and m.FundList is a pointer that is only cleared when I shut down the main form (in the shutdown() function).

Anyway, I think your answer, that it should work, is good enough for me right now. Either something weird is happening in GoTK3, or more likely, I have made some stupid mistake in the code somewhere that I might (or might not) find in the future. The important thing is that I have a workaround for now (the three liner).

The reason I asked the question, was that I was afraid that I had missed some point about receivers and pointers, that I probably should have known about :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.