Attempt to index global error

I'm new to Lua/Corona and need some assistance. Can someone explain why code snippit 1 but not code snippit 2 generates the following error:
main.lua:21: attempt to index global 'myButton' (a nil value)

--Code snippit 1
local button = {myButton,"button1.png",80,190}
-- Format of the table above is button name, image filename, x coordinate, y coordinate

---[[
for i = 1,1 do
b=button[i]
f=button[i+1]
x=button[i+2]
y=button[i+3]
b = display.newImage(f)
b.x=x
b.y=y
end
--]]

--[[
local myButton =

display.newImage( "button1.png" )
myButton.x = 80
myButton.y = 190
--]]

function myButton:tap(event)
p=p+1
return true
end

myButton:addEventListener("tap",myButton)

-----------------------------------

--Code snippit 2
local button = {myButton,"button1.png",80,190}
-- Format of the table above is button name, image filename, x coordinate, y coordinate

--[[
for i = 1,1 do
b=button[i]
f=button[i+1]
x=button[i+2]
y=button[i+3]
b = display.newImage(f)
b.x=x
b.y=y
end
--]]

---[[
local myButton = display.newImage( "button1.png" )
myButton.x = 80
myButton.y = 190
--]]

function myButton:tap(event)
p=p+1
return true
end

myButton:addEventListener("tap",myButton)

In the first snippit, the line where "myButton" gets created is commented out.

Also note that in both snippits, in the first line:

local button = {myButton,"button1.png",80,190}

'myButton' does not exist yet, so 'nil' is being placed in the table.

HTH

then how to overcome that error ...please help me in this regard

views:1450 update:2011/10/3 8:06:12
corona forums © 2003-2011