Creating Objects In A Loop - Can't Add Event Listener?

I’m trying to use this code:

local a = {}
local b = {}

for i = 1,4 do
 a[i] = display.newImage("a.png")
 a[i].x = math.random(100)
 physics.addBody(a[i], {bounce=0.75, radius=50})
 a[i]:applyAngularImpulse(10)
 a[i]:applyLinearImpulse(xrand, yrand, a[i].x, a[i].y)
 a[i]:addEventListener("touch", eventA)

 b[i] = display.newImage("b.png")
 b[i].x = math.random(100)
 physics.addBody(b[i], {bounce=0.75, radius=50})
 b[i]:applyAngularImpulse(10)
 b[i]:applyLinearImpulse(xrand, yrand, b[i].x, b[i].y)
 b[i]:addEventListener("touch", eventB)
end

and I have two event procedures, eventA and eventB that are defined outside the loop.

Why do “applyAngularImpulse” and “applyLinearImpulse” work on a and b but not “addEventListener”?

If I comment out the two lines with “addEventListener” it works, otherwise I get this error:

Runtime error: assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
/Users/username/Desktop/project/main.lua:44: in main chunk

Thanks in advance. [import]uid: 20731 topic_id: 5268 reply_id: 305268[/import]

Just making sure, the functions eventA and eventB are declared above the for loop? [import]uid: 8444 topic_id: 5268 reply_id: 17428[/import]

Nope! They were below… too much Python!

Thanks - that did it! [import]uid: 20731 topic_id: 5268 reply_id: 17430[/import]

Oh good! Yeah, that has tripped me up too! [import]uid: 8444 topic_id: 5268 reply_id: 17431[/import]

u can also forward declare

at the top you could have just said

local eventA, eventB

blahblahlbha
eventA = function (… )

end

c. [import]uid: 24 topic_id: 5268 reply_id: 17441[/import]