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

attempt to index ? (a nil value)

Started by matt2887, 19 June 2014 - 04:55 PM
matt2887 #1
Posted 19 June 2014 - 06:55 PM
So I'm helping a friend with a menu program. It prints the menu options just fine, it prints the >> to show the selected menu item just fine, however when you hit the up down or enter it gives an error 'menu:30: attempt to index ? (a nil value). I am completely stumped.
Spoiler
[list=1]
[*]--[[ Local Variables]]--
[*]
[*]local termWidth, termHeight = term.getSize()
[*]local selectedItem = 1
[*]local running = true
[*]
[*]--[[ Menu Definitions]]--
[*]
[*]mainMenu = {
[*][1] = { text = "Choice 1", handler = Choice1 },
[*][2] = { text = "Choice 2", handler = Choice2 },
[*][3] = { text = "Exit", handler = Exit }
[*]}
[*]
[*]--[[ Printing Methods ]]--
[*]
[*]function printMenu( menu )
[*]for i=1,#menu do
[*]if i == selectedItem then
[*]	  print(">> ".. menu[i].text)
[*]else
[*]	  print("   ".. menu[i].text)
[*]end
[*]end
[*]end
[*]
[*]--[[ Handler Methods ]]--
[*]
[*]function onKeyPressed( key, menu )
[*]if key == keys.enter then --getting an attempt to index a nil value here
[*]onItemSelected(menu)
[*]elseif key == keys.up then
[*]if selectedItem > 1 then
[*]	  selectedItem = selectedItem - 1
[*]end
[*]elseif key == keys.down then
[*]if selectedItem < #menu then
[*]	  selectedItem = selectedItem + 1
[*]end
[*]end
[*]end
[*]
[*]function onItemSelected( menu )
[*]menu[selectedItem].handler()
[*]end
[*]
[*]--[[ Main Method ]]--
[*]
[*]function main()
[*]term.clear()
[*]term.setCrusorPos(1,1)
[*]printMenu(mainMenu)
[*]
[*]event, key = os.pullEvent("key")
[*]onKeyPressed(key,mainMenu)
[*]end
[*]
[*]main()
[/list]
RoD #2
Posted 19 June 2014 - 07:42 PM
Hey! :D/>
First i had to take all those [*]things.. not hard but could be avoided…
Second you need to redraw the menu
Third functions under the caller will not run, so, if you want to use a function make sure you are calling it after having the function written above.
Fourth The handlers in you table… lead to nothing. I did the functions . Hope you get it.

Basically your hanlers will lead to a function. Each option calls the function.
Here you have the code i edited. Warning: its not organized, i messed up and changed things:

--[[ Local Variables]]--
local termWidth, termHeight = term.getSize()
local selectedItem = 2
local running = true
--[[ Menu Definitions]]--
function choice1()
    term.setCursorPos(10,10)
    print("YOU HAVE CHOOSEN ME")
    sleep(1)
end
function choice2()
    term.setCursorPos(10,10)
    print("YOU HAVE CHOOSEN THE SECOND")
    sleep(1)
end
function exit()
    term.setCursorPos(10,10)
    print("YOU HAVE CHOOSEN TO EXIT")
    sleep(1)
end
mainMenu = {
[1] = { text = "Choice 1", handler = choice1  },
[2] = { text = "Choice 2", handler = choice2 },
[3] = { text = "Exit", handler = exit }
}
--[[ Printing Methods ]]--
function printMenu( menu )
 
for i=1,#menu do
if i == selectedItem then
	   print(">> ".. menu[i].text)
else
	   print("   ".. menu[i].text)
end
end
end
function onItemSelected( menu )
print(menu[selectedItem].handler())
sleep(1)
end
--[[ Handler Methods ]]--
function onKeyPressed( key, menu )
if key == keys.enter then --getting an attempt to index a nil value here
onItemSelected(menu)
elseif key == keys.up then
    print("Up")
if selectedItem > 1 then
    print("set")
	   selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
	   selectedItem = selectedItem + 1
end
end
term.clear()
term.setCursorPos(1,1)
printMenu( menu )
end
--[[ Main Method ]]--
function main()
term.clear()
term.setCursorPos(1,1)
printMenu(mainMenu)
while true do
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end
main()
matt2887 #3
Posted 19 June 2014 - 10:10 PM
I copied and pasted your code above into a file in my single player world. Still getting the same error on the same line. the only difference now is the >> that shows the selected menu item starts off in the middle, pointing at choice 2 instead of at the top pointing at choice 1.
RoD #4
Posted 19 June 2014 - 10:20 PM
yeas i did change the initial variable from 1 to 2 to test a thing. You can change it back again.
It works like a charm on mine… O.o
Have you tryed to use the keys and press enter? If nothing happens post your cc version here pls.
Bomb Bloke #5
Posted 20 June 2014 - 05:44 AM
It's saying that it doesn't like you attempting to refer to index "enter" inside of "keys", on the basis that "keys" is nil.

However, "keys" should not be nil - it should be a table. It seems that at some point you've overwritten that global "keys" table (bearing in mind that such a mistake would not be undone when the offending script ended, due to the way variable localisation/globalisation works in Lua).

Reboot your computer and test the script again - this should restore "keys" back to its original state, assuming you don't have a startup script messing with it.
matt2887 #6
Posted 20 June 2014 - 06:04 AM
CC mod version 1.33.
I have tried in fresh SSP world before and after I restarted my comp. no change. I also manually rewrote the code in the SMP world i play in tekkitnoodle. No changes.
Bomb Bloke #7
Posted 20 June 2014 - 06:09 AM
CC mod version 1.33

Ah - there's your problem. There was no "keys" API until CC 1.4.

You may be able to get away with using the numeric key values from this page instead.
matt2887 #8
Posted 20 June 2014 - 06:18 AM
Omg i fixed it :)/>
by replacing keys.enter with the numeric value of 28, the keys.up with 200, and the keys.down with 208 it works perfectly

That's HILARIOUS, that's the page i found :)/>

"As of ComputerCraft 1.52, the following constants are defined:" quoted from the keys API page, i figured the version of CC was too old to have that API.

Thanks again everyone!
Edited on 20 June 2014 - 04:18 AM
Bomb Bloke #9
Posted 20 June 2014 - 06:28 AM
Hrm. That line's probably a bit misleading - makes it sound like the "original" version was introduced then.

Well done in any case. :)/>
RoD #10
Posted 20 June 2014 - 05:25 PM
I tested this in a version with the keys, and it idnt worked. I needed to changes a couple of things.