Attempt to compare nil with number - Cannot Find a Fix

Hello,

This is my first game using Corona, and the problem I’m having, as you can tell from the title, is the error that keeps telling me “attempt to compare nil with number.” The code is based off of the getting started spaceship project with a bunch of modifications, but I’ve been searching for hours up hours and I’m really stuck here. Here’s the part of my code that seems to have the issue:

local function ballCleaner() for i = #ballsTable, 1, -1 do &nbsp; &nbsp; &nbsp; &nbsp; local thisBall = ballsTable[i] if (thisBall.x \< -100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisBall.x \> display.contentWidth + 100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisBall.y \< -100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisBall.y \> display.contentHeight + 100) &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.remove( thisBall ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.remove( ballsTable, i ) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end end



local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if (( obj1.myName == "orange" and obj2.myName == "circle" ) or ( obj1.myName == "circle" and obj2.myName == "orange" ) ) then -- Remove both the circle and the orange&nbsp; display.remove( obj1 ) display.remove( obj2 ) timer.performWithDelay(10, endGame) timer.cancel(scoreTimer) for i = #ballsTable, 1, -1 do if ( ballsTable[i] == obj1 or ballsTable[i] == obj2 ) then ballsTable[i] =&nbsp;nil break end end end end end

The first block: basically a function that cleans up the objects that go off the screen.

The second block: when the circle and the orange collide, they both disappear, the game is ended, the score/timer stops and the game is ended.

The problem I believe stems from when the circle collides with the orange, then they both disappear and the orange is removed from “ballsTable” (sorry if the naming is a bit confusing)/changed to nil. Thus, it can’t be compared in the first block’s if then statement because it doesn’t have an .x or .y value. I’ve played around with a bunch of ideas and solutions but I think it’s time that I sought some help. Thanks so much!

The problem is in the way your a trying to manage objects with a table.
 
You’re using numeric indices and simply setting the value to nil instead of removing it from the table.
 
There are two basic ways to index tables: numerically and by reference
 
Numerically indexed tables done WRONG

-- Numerically indexed table local stuff = {} stuff[1] = display.newCircle(10,10,10) stuff[2] = display.newCircle(10,10,10) stuff[3] = display.newCircle(10,10,10) display.remove(stuff[2]) stuff[2] = nil -- stuff[2] is now nil, but still present so iterating over stuff will return nil from index 2.

Numerically indexed tables done RIGHT-ish

-- Numerically indexed table better, but not good for iterators local stuff = {} stuff[1] = display.newCircle(10,10,10) stuff[2] = display.newCircle(10,10,10) stuff[3] = display.newCircle(10,10,10) display.remove(stuff[2]) table.remove( stuff, 2 ) 

 The problem is, you can’t do this kind of thing in the middle of iterating over a table or you will break the iterator.

Thus, I prefer tracking objects by reference.

Reference Indexed Tables

-- Reference indexed table local stuff = {} local tmp = display.newCircle(10,10,10) stuff[tmp] = tmp local tmp = display.newCircle(10,10,10) stuff[tmp] = tmp local tmp = display.newCircle(10,10,10) stuff[tmp] = tmp -- iterate over this table like this: for k,v in pairs( stuff ) do -- do something with v -- deleting an object is as simple as: display.remove( v ) stuff[k] = nil end

Looks like thing’s are working, thank you so much for your explanation!

The problem is in the way your a trying to manage objects with a table.
 
You’re using numeric indices and simply setting the value to nil instead of removing it from the table.
 
There are two basic ways to index tables: numerically and by reference
 
Numerically indexed tables done WRONG

-- Numerically indexed table local stuff = {} stuff[1] = display.newCircle(10,10,10) stuff[2] = display.newCircle(10,10,10) stuff[3] = display.newCircle(10,10,10) display.remove(stuff[2]) stuff[2] = nil -- stuff[2] is now nil, but still present so iterating over stuff will return nil from index 2.

Numerically indexed tables done RIGHT-ish

-- Numerically indexed table better, but not good for iterators local stuff = {} stuff[1] = display.newCircle(10,10,10) stuff[2] = display.newCircle(10,10,10) stuff[3] = display.newCircle(10,10,10) display.remove(stuff[2]) table.remove( stuff, 2 ) 

 The problem is, you can’t do this kind of thing in the middle of iterating over a table or you will break the iterator.

Thus, I prefer tracking objects by reference.

Reference Indexed Tables

-- Reference indexed table local stuff = {} local tmp = display.newCircle(10,10,10) stuff[tmp] = tmp local tmp = display.newCircle(10,10,10) stuff[tmp] = tmp local tmp = display.newCircle(10,10,10) stuff[tmp] = tmp -- iterate over this table like this: for k,v in pairs( stuff ) do -- do something with v -- deleting an object is as simple as: display.remove( v ) stuff[k] = nil end

Looks like thing’s are working, thank you so much for your explanation!