Error: bad argument #1 to 'insert' (table expected, got nil)"

Hello,

I’m trying to learn to code with Corona so I’m currently messing around with the code but I need some help since I’m getting an error and I can’t seem to figure out what’s wrong with it…?

“Main.lua:61:bad argument #1 to ‘insert’ (table expected, got nil)”

Below you will find a piece of the code.

I find it strange that it gives errors since I’m using the code from one of the tutorials on the site.

Can someone spot what I’m doing wrong here?

Many thanks.

local function createFish()

    local newFish = display.newImageRect( “fish.png”, 20, 20)
    table.insert( fishTable, newFish )
    physics.addBody( newFish, “dynamic”, { radius=10, bounce=0.8 } )
    newFish.myName = “fish”
    
    newFish:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )
    newFish.x = display.contentWidth + 60
    newFish.y = math.random( 500 )
    newFish:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )
    
end

local function gameLoop()

    – Create new fish
    createFish()

    – Remove asteroids which have drifted off screen
    for i = #fishTable, 1, -1 do
        local thisFish = fishTable[i]

        if ( thisFish.x < -100 or
             thisFish.x > display.contentWidth + 100 or
             thisFish.y < -100 or
             thisFish.y > display.contentHeight + 100 )
        then
            display.remove( thisFish )
            table.remove( fishTable, i )
        end
    end
end

gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 )

Hello @anion and welcome to the forums.  Since you’re new here, let me cover a couple of topics before I get to the question. 

First - posting code: You’ve done great showing us the code where the problem is. But we recommend you use code formatting to post your code. You can do that in one of two ways. You can type:

[lua] paste your code here [/lua]

or you can click the blue <> button in the edit bar with Bold, Italic, etc. and paste your code into the popup window. This will make it easier on community members who want to help.

Secondly, when posting about errors and you have a line number like “61” in this case, we don’t have a good point of reference as to what line 61 is. In this case it tells me its a problem with an  insert call and you only have one, but it’s possible you have other insert commands and the one above isn’t line 61.  If you could point out which line is causing the problem that would be extra helpful too.  You could do this as simply as putting a comment at the end of the offending line:   --<------ This is line 61    for example.

Now to the problem, assuming this line:

table.insert( fishTable, newFish )

is line 61, then the message says:  Bad argument #1. That’s fishTable. And the error is telling you that it’s nil and expecting a table. So you have to now ask yourself: “Why is fishTable nil?” You haven’t posted any code showing where you’ve declared  fishTable to be a table/array.  Somewhere above that in your code you probably need to have:

local fishTable = {}

to make it an table/array.

Hope that helps you find the error.

Rob

Hi Rob,

I’ll will keep your tips about the code posting in mind.

Your suggestion about adding that piece of code was indeed the solution. Still learning the code so it 's easy to overlook something like this declaration :slight_smile:

Anyway, it works now so thanks for your help!

Hello @anion and welcome to the forums.  Since you’re new here, let me cover a couple of topics before I get to the question. 

First - posting code: You’ve done great showing us the code where the problem is. But we recommend you use code formatting to post your code. You can do that in one of two ways. You can type:

[lua] paste your code here [/lua]

or you can click the blue <> button in the edit bar with Bold, Italic, etc. and paste your code into the popup window. This will make it easier on community members who want to help.

Secondly, when posting about errors and you have a line number like “61” in this case, we don’t have a good point of reference as to what line 61 is. In this case it tells me its a problem with an  insert call and you only have one, but it’s possible you have other insert commands and the one above isn’t line 61.  If you could point out which line is causing the problem that would be extra helpful too.  You could do this as simply as putting a comment at the end of the offending line:   --<------ This is line 61    for example.

Now to the problem, assuming this line:

table.insert( fishTable, newFish )

is line 61, then the message says:  Bad argument #1. That’s fishTable. And the error is telling you that it’s nil and expecting a table. So you have to now ask yourself: “Why is fishTable nil?” You haven’t posted any code showing where you’ve declared  fishTable to be a table/array.  Somewhere above that in your code you probably need to have:

local fishTable = {}

to make it an table/array.

Hope that helps you find the error.

Rob

Hi Rob,

I’ll will keep your tips about the code posting in mind.

Your suggestion about adding that piece of code was indeed the solution. Still learning the code so it 's easy to overlook something like this declaration :slight_smile:

Anyway, it works now so thanks for your help!