sometimes create new group fail

 hello all,

here is my code,I call the addenermy function periodically,

but it sometimes successful,sometimes error as picture,

the red line below is line 156,

thanks for your help

timesource = timer.performWithDelay(math.random(800,1200),addenermy,0)

function addenermy()

    local j = math.random(1,10)

    local str1 = “images/monster/mon”…tostring(j)…".png"

    local x,y = math.random(50,_W-50),-200

    local mon = display.newImage(str1)

    local i = math.random(1,5)

    local str = “images/small/c_”…kindofgesture[i]…".png"

    local core = display.newImage(str)

    local enermy = display.newGroup()

    enermy:insert(mon)

    enermy:insert(core)

    enermy.x,enermy.y = x,y

    enermy.type = kindofgesture[i]

    physics.addBody(enermy,{radius = 50})

    enermy:setLinearVelocity(math.random(0,50),math.random(200,400))

    enermygroup:insert(enermy)

end

Note: Please use the code comment block button in the toolbar!

If the line in red is throwing an error it means that ‘mon’ is nil. ‘mon’ will be nil if this line fails:

local mon = display.newImage(str1)

That line will fail (it actually returns nil) if this line fails:

local str1 = "images/monster/mon"..tostring(j)..".png"

This line fails because the string it generates does not refer to a file in your app.

You should do this to check the filenames it is generating and check that they all exist in your app:

print(str1)

!!! I am fucking stupid…

Note: Please use the code comment block button in the toolbar!

If the line in red is throwing an error it means that ‘mon’ is nil. ‘mon’ will be nil if this line fails:

local mon = display.newImage(str1)

That line will fail (it actually returns nil) if this line fails:

local str1 = "images/monster/mon"..tostring(j)..".png"

This line fails because the string it generates does not refer to a file in your app.

You should do this to check the filenames it is generating and check that they all exist in your app:

print(str1)

!!! I am fucking stupid…