This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
eniallator's profile picture

index expected, instead got nil

Started by eniallator, 17 January 2016 - 10:15 PM
eniallator #1
Posted 17 January 2016 - 11:15 PM
I've been working on a new feature for my Remote/Control programs and have tried to implement it into my control program. the error im running into is this: mControl:135: index expected, instead got nil

Here is the pastebin for the program and Here is the plugin its trying to load. I also have a couple of screenshots Here (the reason why it says 137 in the screenshot is because of the 2 prints i have added before it).
KingofGamesYami #2
Posted 18 January 2016 - 12:04 AM
Basically you're trying to index a value which isn't a table (instead, it's nil).

Line 135:

_G[tabName][key]

_G clearly isn't nil.

_G[ tabName ] could very well be nil. I would consider using a print on this, to check when it's nil and why.
eniallator #3
Posted 18 January 2016 - 12:20 AM
Basically you're trying to index a value which isn't a table (instead, it's nil).

Line 135:

_G[tabName][key]

_G clearly isn't nil.

_G[ tabName ] could very well be nil. I would consider using a print on this, to check when it's nil and why.

did you see the imgur link to the ingame screenshots i posted aswell? i've printed mergeTab, tabName, key and value and they all are what they should be
Dragon53535 #4
Posted 18 January 2016 - 12:36 AM
Well obviously not if it's erroring on you. It's best to make sure, add in an if statement.


if _G[tabName] then
  _G[tabName][key] = value
else
  print("Aww shucks, it's nil")
end
Edited on 17 January 2016 - 11:38 PM
Bomb Bloke #5
Posted 18 January 2016 - 06:51 AM
That's odd, are you sure it's not line 134 throwing the error? That particular message isn't something I'd expect from that particular line, though pairs() could throw it.
eniallator #6
Posted 18 January 2016 - 08:59 AM
Well obviously not if it's erroring on you. It's best to make sure, add in an if statement.


if _G[tabName] then
  _G[tabName][key] = value
else
  print("Aww shucks, it's nil")
end

just tried that and its always going to the else statement and not the code in the if.

That's odd, are you sure it's not line 134 throwing the error? That particular message isn't something I'd expect from that particular line, though pairs() could throw it.

well its saying the line after so if it means the line before then yes but wouldn't that error/nil when i tried to print it?
Bomb Bloke #7
Posted 18 January 2016 - 09:09 AM
Yeah, you're quite right, it's because you never put the tables you're trying to index into _G.

Defining them as global tables (as you do around line 38) doesn't achieve that, at least, not within ComputerCraft. Each system has a global environment table that's separate to _G. If you want to stick something in _G, specify that that's where you want it to go.

Which brings up the question, why would you want to stick it them _G? Why not just make them subtables of something local to your script?
eniallator #8
Posted 18 January 2016 - 09:15 AM
Yeah, you're quite right, it's because you never put the tables you're trying to index into _G.

Defining them as global tables (as you do around line 38) doesn't achieve that, at least, not within ComputerCraft. Each system has a global environment table that's separate to _G. If you want to stick something in _G, specify that that's where you want it to go.

Which brings up the question, why would you want to stick it them _G? Why not just make them subtables of something local to your script?

How would i specify thats where i want it to go? and im not sure what you mean by that last bit :P/>
Edited on 18 January 2016 - 08:50 AM
Bomb Bloke #9
Posted 18 January 2016 - 11:35 AM
How would i specify thats where i want it to go?

Instead of:

status = {upload = 1, download = 1, filelist = 1, redstone = 1}

… you'd do:

_G.status = {upload = 1, download = 1, filelist = 1, redstone = 1}

and im not sure what you mean by that last bit :P/>

local myTable = {}
myTable.status = {upload = 1, download = 1, filelist = 1, redstone = 1}
eniallator #10
Posted 18 January 2016 - 09:11 PM
How would i specify thats where i want it to go?

Instead of:

status = {upload = 1, download = 1, filelist = 1, redstone = 1}

… you'd do:

_G.status = {upload = 1, download = 1, filelist = 1, redstone = 1}

and im not sure what you mean by that last bit :P/>

local myTable = {}
myTable.status = {upload = 1, download = 1, filelist = 1, redstone = 1}

i made another solution for the problem and Here is the result of it, Thank you so much for the help though, i don't think i was even close to finding the solution ;)/>