Attempt to index field '?' (a nil value)

Here is the error:


Corona Runtime Error


lib\loadLevel.lua:27: attempt to index field ‘?’ (a nil value)

stack traceback:

lib\loadLevel.lua:27: in function ‘load’

code\gameScene.lua:22: in function ‘?’

?: in function ‘dispatchEvent’

?: in function ‘gotoScene’

code\levelSelect.lua:26: in function ‘_onEvent’

?: in function ‘?’

?: in function ‘?’

?: in function <?:182>

Do you want to relaunch the project?


Yes   No   


So here is my problem: The error occurs only when I am trying to go from scene main.lua > scene  code.levelSelect > scene code.gameScene BUT it does not happen if I call scene main.lua > scene code.gameScene.

Here is my code.gameScene:

local composer = require ("composer") local scene = composer.newScene("scene") local settings = require("code.settings") local loadLevel = require("lib.loadLevel") function scene:create(event) -- Init some important vars local view = self.view local \_X = display.contentCenterX local \_Y = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight -- Activate multitouch system.activate("multitouch") -- Create background local background = display.newRect(\_X, \_Y, display.actualContentWidth, \_H) view:insert(background) loadLevel.load() loadLevel.movePlayer() end function scene:show(event) end function scene:hide(event) end function scene:destroy(event) end scene:addEventListener("create") scene:addEventListener("show") scene:addEventListener("hide") scene:addEventListener("destroy") return scene

Here is part of my lib.loadLevel:

local currentLevel = settings.m.currentLevel for i = 1, 50 do if True == true then if levelData.level[currentLevel][i] == nil then -- this is where the error occurs num = i - 1 True = false end end end

Here is my code.levelData:

local M = {} local settings = require("code.settings") M.level = {} local curr = settings.m.currentLevel local \_X = display.contentCenterX local \_Y = display.contentCenterY M.level[1] = { { Type = "ball", x = \_X-200, y = \_Y, r = 20, c = {1,0,0} }, --{ Type = "obstacle", x = \_X, y = \_Y + 100, w = 50, h = 200, c = {0,0,0} }, --{ Type = "obstacle", x = \_X - 50, y = \_Y + 200, w = 50, h = 200, c = {0,0,0} }, { Type = "portal1", x = \_X, y = \_Y + 100, r = 30, c = {1,1,0}}, { Type = "portal2", x = \_X, y = \_Y - 100, r = 30, c = {0,0,1}}, } return M

Please help me! I have tried to debug this but I have not found a way!

You only have one level, so make sure the value of settings.m.currentLevel is actually 1 before you start traversing your list. Then the table entries you’re indexing with “i” can be in a range of 1 to 50, but you only have three items in the M.level[1] table. Perhaps change your index for loop to:

for i = 1,&nbsp;#levelData.level[currentLevel]&nbsp;do

instead. 

You should be printing out the values of your index values (i, currentLevel) to see which one is nil on you or causing you to look up a nil record in the table.

Rob

Ok, I kind of figured this out. Sorry Rob, but your answer did not work out. Ok. So I found out that instead of creating a local variable 

local currentLevel = settings.m.currentLevel

I must declare the current level to be 1

local currentLevel = 1

Also, I have tried to:

local currentLevel = unpack(settings.m.currentLevel)

which returns bad argument #1 to ‘unpack’ (table expected, got string).

And I have also tried:

local currentLevel = table.concat(settings.m.currentLevel)

which returns bad argument #1 to ‘concat’ (table expected, got string)

So the conclusion I made is that something changes the integer into string and by that, it does not work.

If anyone would help, I’d appreciate it.

Thanks

Ok this is the fix:

-- It is somehow a string, so I need to make it integer local currentLevel = tonumber(settings.m.currentLevel)

Here are the docs:

https://docs.coronalabs.com/api/library/global/tonumber.html

You only have one level, so make sure the value of settings.m.currentLevel is actually 1 before you start traversing your list. Then the table entries you’re indexing with “i” can be in a range of 1 to 50, but you only have three items in the M.level[1] table. Perhaps change your index for loop to:

for i = 1,&nbsp;#levelData.level[currentLevel]&nbsp;do

instead. 

You should be printing out the values of your index values (i, currentLevel) to see which one is nil on you or causing you to look up a nil record in the table.

Rob

Ok, I kind of figured this out. Sorry Rob, but your answer did not work out. Ok. So I found out that instead of creating a local variable 

local currentLevel = settings.m.currentLevel

I must declare the current level to be 1

local currentLevel = 1

Also, I have tried to:

local currentLevel = unpack(settings.m.currentLevel)

which returns bad argument #1 to ‘unpack’ (table expected, got string).

And I have also tried:

local currentLevel = table.concat(settings.m.currentLevel)

which returns bad argument #1 to ‘concat’ (table expected, got string)

So the conclusion I made is that something changes the integer into string and by that, it does not work.

If anyone would help, I’d appreciate it.

Thanks

Ok this is the fix:

-- It is somehow a string, so I need to make it integer local currentLevel = tonumber(settings.m.currentLevel)

Here are the docs:

https://docs.coronalabs.com/api/library/global/tonumber.html