Paranormal display.newGroup():insert() function bug activity

I have noticed strange bugs with display.newGroup():insert() function.

I had this function:
[lua]function orderObjects(field)
orderGroup = display.newGroup()

local main_obj = {}
for i = 1, field.ROWS do
for k = 1, field.COLS do
if field.cells[k][i].object then
if field.cells[k][i].object.type ~= “main” then
orderGroup:insert(orderGroup.numChildren + 1, field.cells[k][i].object.image)
else
main_obj = field.cells[k][i].object
end
end
end
end
orderGroup:insert(orderGroup.numChildren + 1, main_obj.image)
end[/lua]

In this case function call orderGroup:insert(orderGroup.numChildren + 1, main_obj.image) gave me "ERROR: table expected. If this is a function call you might use ‘.’ extead of ‘:’ "
What’s this? main_obj.image is not nil.

Then, to avoid this bug I refactored my code and wrote this:
[lua]function orderObjects(field)
orderGroup = display.newGroup()

for i = 1, field.ROWS do
for k = 1, field.COLS do
if field.cells[k][i].object then
orderGroup:insert(orderGroup.numChildren + 1, field.cells[k][i].object.image)

if field.cells[k][i].object.stands_on
and field.cells[k][i].object.stands_on.type == “main” then
orderGroup:insert(#field.objects, field.cells[k][i].object.stands_on.image)
end
end
end
end
end[/lua]

In this case at function call orderGroup:insert(#field.objects, field.cells[k][i].object.stands_on.image) Corona simulator just crashed permanently.

I have a build 2011.591 [import]uid: 13867 topic_id: 17243 reply_id: 317243[/import]

I cannot comprehend the complexity you are introducing, but a simple suggestion, why do you *not* check as

if main\_obj.image ~= nil then  
 -- do the inserting here  
end  

there are even more advanced methods to check if it is a display object or not, and so on. For the scope of this query, let’s just check for nil.
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17243 reply_id: 65147[/import]