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]