Error inserting display group into a scrollview - Attempt to call method 'insert' (a nil value)

I have a method which returns search results. I have a Runtime event listener which is used to monitor if the user has changed the search term, if the search term changes then:

  • remove the previous results 
  • create a new results display group
  • attempt to insert the new group into the scrollview

This seems to work when you enter the scene the first time. However, I’m getting a “Attempt to call method ‘insert’ (a nil value)” if I go out and come back in. I’ve made sure I’m removing all Runtime listeners in the destroy scene event and have verified the destroy scene event is getting called each time I leave the scene.

scene.lua (scrollview

local scrollView ... ... function scene:show( event ) local group = self.view ... scrollView = widget.newScrollView { top = 0, left = 0, width = display.contentWidth, height = display.contentHeight, isBounceEnabled = true, isLocked = false, horizontalScrollDisabled = true, backgroundColor = { 0, 0, 0, 0 }, listener = scrollListener } ... group:insert(scrollView) ... search.start(rY, scrollView)

search.lua

function search.resetResults( rY, scrollView ) -- resetResults gets called by search.start if user has changed the search term ... 154 resultGroup:removeSelf() 155 resultGroup = nil 156 resultGroup = display.newGroup() 157 resultGroup.y = rY 158 resultGroup.curY = 10 159 if scrollView and resultGroup then 160 print("resultGroup.y", resultGroup.y) 161 print("scrollView.\_widgetType", scrollView.\_widgetType) 162 scrollView:insert(resultGroup) 163 end

Note - I’ve added the if statement above to verify that both scrollView and the resultGroup exist before I try to insert resultGroup into the scrollView.

From the console:

resultGroup.y 936.97998046875

scrollView._widgetType scrollView

ERROR: Runtime error

                    ?:0: attempt to call method ‘insert’ (a nil value)

                    stack traceback:

                    ?: in function ‘insert’

                    search.lua:162: in function ‘resetResults’

                    search.lua:51: in function ‘?’

                    ?: in function <?:190>

Using Corona Version 2018.3326 (2018.6.25)

Can you boil this down more to reproduce the problem with a simpler program?

Could you reduce it to two scenes and post the entire listing?

I’ve seen this sort of thing before and it is always because of some unnoticed, little assumption made somewhere else which throws out a variable.

I suspect that the scrollView object you are trying to call insert on is not the same as the one which you are actually trying to call it on. I mean: You are probably trying to clean the scene up and not doing it completely. Any time a function doesn’t exist where it used to on a display object is because it’s the wrong object being used - it got cleaned up but not removed from memory.

Is there a reason you are using:

search.start(rY, scrollView)

and not:

search:start(rY, scrollView)

?

Can you boil this down more to reproduce the problem with a simpler program?

Could you reduce it to two scenes and post the entire listing?

I’ve seen this sort of thing before and it is always because of some unnoticed, little assumption made somewhere else which throws out a variable.

I suspect that the scrollView object you are trying to call insert on is not the same as the one which you are actually trying to call it on. I mean: You are probably trying to clean the scene up and not doing it completely. Any time a function doesn’t exist where it used to on a display object is because it’s the wrong object being used - it got cleaned up but not removed from memory.

Is there a reason you are using:

search.start(rY, scrollView)

and not:

search:start(rY, scrollView)

?