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

Attempt to Index (a nil value)

Started by Strite, 28 March 2014 - 07:00 PM
Strite #1
Posted 28 March 2014 - 08:00 PM
Hello World! Er, so I'm trying to build OS - It's an extremely simple one, text based only, no mouse and/or keys, only a couple of programs, it's not complicated at all. I just wanted a challenge of some sort, and this is what've come up with.

Anyway, I encountered this problem while coding: "striteos:39: attempt to index ? (a nil value)", and I can't really figure out why. And, the error doesn't occur right after the program is iniciated, it only occurs when the user enters the 'edit' panel, a panel I created inside the code itself. Although, the error is telling me that the problem is somewhere around the line 39, which is only a aesthetics part of the code, and it's related at directly to the 'edit' panel. Keep in mind though, the error, apparently, is inside the "centerText" function, which is called when the edit panel starts.

I'm hoping that at least some of you guys can make some sense out of my messy explanation, if not, I'm going to be here all day to answer any type of question.

I also reached my pastebin limits for the day - so I'm actually pasting the code directly here, if that's okay, well, here it is:


local pName = "Strite Operational System"
local version = "0.2"

local programs = {"help", "run", "redstone", "create", "edit", "pastebin", "exit"}

local bSide = "back" --Bundled Cable Side
local tTime = 0.8

--Color Set Functions--
function white()
term.setTextColor(1)
end
function yellow()
term.setTextColor(16)
end
function blue()
term.setTextColor(2048)
end
function green()
term.setTextColor(8192)
end
function red()
term.setTextColor(16384)
end
function black()
term.setTextColor(32768)
end

--Text Functions

function drawLine()
print("---------------------------------------------------")
end

function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos() --Here
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end

function drawLogo(text)
red()
drawLine()
white()
centerText(text)
red()
drawLine()
white()
end

function drawSubLogo(text)
centerText(text)
red()
drawLine()
white()
end

function drawTopLogo(text)
red()
drawLine()
white()
centerText(text)
end

function clear()
term.clear()
term.setCursorPos(1,1)
end

function press()
red()
centerText("~")
yellow()
centerText("Press anything to continue!")
white()
os.pullEvent("key")
sleep(0.1)
end

function drawPackA(first, second)
clear()
drawLogo(pName)
drawSubLogo(first)
centerText(second)
end

function drawPackB(first)
clear()
drawLogo(pName)
drawSubLogo(first)
end

function drawPackC(first, second, third)
clear()
drawLogo(pName)
drawSubLogo(first)
centerText(second)
print()
centerText(third)
end


--Main Programs
function drawMain()
clear()
drawLogo(pName)
drawSubLogo("~Version "..version.."!~")
print()
red()
write("> ")
white()
local com = string.lower(read())
  if com == "redstone" or com == "rs" then
  redstone()
  elseif com == "exit" or com == "out" then
  exit()
  elseif com == "help" or com == "programs" then
  help()
  elseif com == "edit" then
  edit()
  elseif com == "pastebin" then
  pastebin()
  elseif com == "create" or com == "new" then
  create()
  elseif com == "run" or com == "go" then
  run()
  elseif com == "config" or com == "settings" then
  config()
  else
  print()
  centerText("Unknown command, please try again!")
  print()
  press()
  drawMain()
  end
end

function run()
drawPackC("Execute Management Tab!", "Please, type the name of the program", "you want to run.")
print()
print()
red()
write("> ")
white()
local rName = read()
  if rName == "exit" or rName == "Exit" then
  drawMain()
  else
	if fs.exists(rName) then
	clear()
	shell.run(rName)
	press()
	drawMain()
	else
	drawPackA("Execute Management Tab!", "That program does not exist.")
	print()
	print()
	print()
	centerText("Please, try again.")
	print()
	press()
	run()
	end
  end
end

function config()
drawPackC("Configuration Tab!", "Please, type the name of the data you", "want to change.")
print()
print()
print("Operational System Name ('osName'): "..pName)
print("Version ('osVersion'): "..version)
print("Transition time ('tTime'): "..tTime) --To do
print("Bundled Cable Side ('bSide'): "..bSide)
print()
red()
write("> ")
white()
local cChange = string.lower(read())
  if cChange == "osname" or cChange == "operational system name" then
  drawPackA("Configuration Tab! - Operational System Name", "Please, type the new name for the OS.")
  print()
  print()
  red()
  write("> ")
  white()
  local newpName = read()
	if newpName == "exit" or newpName == "Exit" then
	drawMain()
	else
	local pName = newpName
	end
  end
end

function rsC(side, colorName, colorNumber)
local rsOnorOff = rs.testBundledInput(side, colorNumber)
  if rsOnorOff == true then
  term.setTextColor(colors.lime)
  centerText(colorName.." Cable Online!")
  elseif rsOnorOff == false then
  term.setTextColor(colors.red)
  centerText(colorName.." Cable Offline!")
  end
white()
print()
end

function rsCI(side)
  rsC(side, "White", 1)
  rsC(side, "Orange", 2)
  rsC(side, "Magenta", 4)
  rsC(side, "Light Blue", 8)
  rsC(side, "Yellow", 16)
  rsC(side, "Lime", 32)
  rsC(side, "Pink", 64)
  rsC(side, "Gray", 128)
  rsC(side, "Light Gray", 256)
end

function rsSwitch(cNum)
local cActive = bit.bor(rs.getBundledInput("left"), rs.getBundledInput(bSide))
rs.setBundledOutput(bSide, cActive+cNum)
end

function rsSwitchOff(cNum)
local cActive = bit.bor(rs.getBundledInput("left"), rs.getBundledInput(bSide))
rs.setBundledOutput(bSide, cActive-cNum)
end

function rsReset()
local cActive = bit.bor(rs.getBundledInput("left"), rs.getBundledInput(bSide))
rs.setBundledOutput(bSide, cActive-cActive)
end

function redstone()
local cablesNum = 6
drawPackA("Redstone Panel Tab!", "-Available Cables-")
print()
  rsCI(bSide)
white()
print()
centerText("Please, type a command.?")
print()
centerText("Ex.: 'on'/'off'/'reset'/'exit'/'refresh'.")
print()
red()
write("> ")
white()
local rsR = string.lower(read())
  if rsR == "exit" then
  term.setCursorPos(1,1)
  drawPackB("Redstone Panel - Exiting")
  sleep(0.8)
  drawMain()
  elseif rsR == "refresh" or rsR == "r" then
  print()
  centerText("Refreshing.")
  sleep(0.8)
  redstone()
  elseif rsR == "on" or rsR == "add" then
  drawPackC("Redstone Panel Tab! - Switch", "Please, type the number of the color you want", "to turn on.")
	print()
  print()
  centerText("Colors List:")
  print()
  centerText("White = 1")
  print()
  centerText("Orange = 2")
  print()
  centerText("Magenta = 4")
  print()
  centerText("Light Blue = 8")
  print()
  centerText("Yellow = 16")
  print()
  centerText("Lime = 32")
  print()
  centerText("Pink = 64")
  print()
  centerText("Gray = 128")
  print()
  centerText("Light Gray = 256")
  print()
  red()
  write("> ")
  white()
  local bOn = read()
  term.setCursorPos(1,1)
  rsSwitch(bOn)
  sleep(0.8)
  redstone()
  elseif rsR == "off" or rsR == "sub" then
  drawPackC("Redstone Panel Tab! - Switch", "Please, type the number of the color you want", "to turn off.")
  print()
  print()
  centerText("Colors List:")
  print()
  centerText("White = 1")
  print()
  centerText("Orange = 2")
  print()
  centerText("Magenta = 4")
  print()
  centerText("Light Blue = 8")
  print()
  centerText("Yellow = 16")
  print()
  centerText("Lime = 32")
  print()
  centerText("Pink = 64")
  print()
  centerText("Gray = 128")
  print()
  centerText("Light Gray = 256")
  print()
  red()
  write("> ")
  white()
  local bNameOff = read()
  term.setCursorPos(1,1)
  rsSwitchOff(bNameOff)
  sleep(0.8)
  redstone()
  elseif rsR == "reset" then
  rsReset()
  sleep(0.8)
  redstone()
  else
  print()
  centerText("Unknown command, refreshing.")
  sleep(0.8)
  redstone()
  end
end

function help()
drawPackA("Help Tab!", "Programs available:")
print()
  for i=1,#programs do
  centerText("-"..programs[i].."-")
  print()
  end
press()
drawMain()
end

function pastebin()
drawPackA("Pastebin Management Tab!", "Please, type: 'get' or 'put'.")
print()
print()
red()
write("> ")
white()
local gop = string.lower(read())
  if gop == "get" or gop == "g" then
   drawPackA("Pastebin Management Tab! - Get", "Please type the pastebin's ID.")
   print()
   print()
   red()
   write("> ")
   white()
   local pastebinID = read()
   drawPackA("Pastebin Management Tab! - Get", "Please type a name for the program.")
   print()
   print()
   red()
   write("> ")
   white()
   local pastebinName = read()
   print()
   drawPackA("Pastebin Management Tab! - Processing", "Please wait.")
   print()
   print()
   shell.run("pastebin get "..pastebinID.." "..pastebinName)
   drawMain()
   elseif gop == "put" then
   drawPackC("Pastebin Management Tab! - Put", "Please type the name of the program", "you want to pastebin.")
   print()
   print()
   red()
   write("> ")
   white()
   local pastebinPut = read()
   drawPackA("Pastebin Management Tab! - Processing", "Please wait.")
   print()
   print()
   shell.run("pastebin put "..pastebinPut)
   local x3,y3 = term.getCursorPos()
   term.setCursorPos(1,4)
   term.clearLine()
   centerText("Pastebin Management Tab! - Done!")
   term.setCursorPos(x3,y3)
   print()
   print()
   centerText("Please, write down your code!")
   print()
   press()
   drawMain()
   end
end

function create()
drawPackA("Creating Files Tab!", "Please, type the name of the file/program.")
print()
red()
write("> ")
white()
local cName = read()
  if cName == "exit" or cName == "Exit" then
  drawMain()
  else
	if fs.exists(cName) then
	term.setCursorPos(1,4)
	term.clearLine()
	centerText("This file already exists!")
	print()
	centerText("Next time, try the 'edit' command!")
	print()
	press()
	drawMain()
	else
	drawPackA("Creating Files Tab! - Done!", "Press anything to create and open the file.")
	print()
	press()
	shell.run("edit "..cName)
	drawMain()
	end
  end
end

function edit()
drawPackC("Editing Files Tab!")
print()
white()
write("File's Name:")
red()
write("> ")
white()
local fname = read()
  if fname == "exit" or fname == "Exit" or fname == "leave" or fname == "Leave" then
  print()
  centerText("Exiting...")
  press()
  drawMain()
  elseif fname == "striteos" then
  print()
  centerText("Keep in mind that StriteOS needs a reboot")
  print()
  centerText("for the changes to take effect!")
  print()
  print()
  centerText("Later on, try using the configs tab!")
  print()
  press()
  shell.run("edit striteos")
  else
	if fs.exists(fname) then
	clear()
	drawLogo(pName)
	drawSubLogo("Opening "..fname.."!")
	sleep(0.6)
	shell.run("edit "..fname)
	drawMain()
	else
	print()
	centerText("Sorry, that file does not exist.")
	print()
	sleep(0.2)
	centerText("You should try the 'create' command!")
	print()
	sleep(0.2)
	centerText("Type 'exit' to get back to the Desktop!")
	print()
	press()
	sleep(0.2)
	edit()
	end
  end
end

function exit()
drawPackC("Are you sure you want to exit StriteOS? (y/n)")
print()
red()
write("> ")
white()
local eyn = string.lower(read())
  if eyn == "y" or eyn == "yes" then
  drawPackC("Exiting StriteOS.")
  sleep(0.8)
  shell.run("reboot")
  elseif eyn == "n" or eyn == "no" then
  drawPackC("Returning to Desktop.")
  sleep(0.8)
  drawMain()
  end
end



--Main
drawMain()

To find line 39, just do "Ctrl + F" and type 'Here'.
TheOddByte #2
Posted 28 March 2014 - 08:12 PM
In the function drawPackC you have 3 parameters, In the edit function you use it with one, Leaving the second nil.
The second parameter is the one that will be drawn to the screen with centeredText, That's why it errors since the second parameter is nil
Edited on 28 March 2014 - 07:13 PM
Strite #3
Posted 28 March 2014 - 08:22 PM
Thanks, that fixed it.