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

java.lang.ArrayIndexOutOfBoundsException

Started by ComputerCraftFan11, 02 April 2012 - 12:07 AM
ComputerCraftFan11 #1
Posted 02 April 2012 - 02:07 AM
I'm trying to make a multibooter but I'm getting 2 problems:

1) Programs don't run
2) When I open a folder thats not rom or if I press /*Back, computercraft crashes


Code:

local getVersion = "0.1"
local x, y = term.getSize()
local currentX = 1;
local currentY = 3;
local Cursor = ">";
local pressed = false;
local running = "";
local installations = 10
local path = ""
local run = true
local backpath = ""
local function cPrint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end
term.clear()
term.setCursorPos(1,1)
function newButton(x1, y1, title, type, do22, filepath)
term.setCursorPos(x1+1, y1)
print(title)
term.setCursorPos(x1+1, y1)
cPrint(type)
if do22 == "redirect" then
  if pressed == true then
   if currentX == x1 then
    if currentY == y1 then
	 backpack = path
	 path = filepath
	 reDraw()
    end
   end
  end
elseif do22 == "back" then
  if pressed == true then
   if currentX == x1 then
    if currentY == y1 then
	 path = backpath
	 reDraw()
    end
   end
  end
else
  if pressed == true then
   if currentX == x1 then
    if currentY == y1 then
	 running = do22
    end
   end
  end
end
end
function drawScreen()
cPrint("MultiBoot " ..getVersion)
local x2,y2 = term.getCursorPos()
term.setCursorPos(1, y2+1)
cPrint("+---------------------------------------+")
newButton(1, 3, "/*Back","<PREVIOUS>", "back", backpath)
files = fs.list(path.. "/")
for n=1,#files do
  local x2, y2 = term.getCursorPos()
  if fs.isDir(path.. "/" ..files[n]) then
   if files[n] == "rom" then
    newButton(1, y2 +1, files[n],"<SYSTEM>", "redirect", path.. "/" ..files[n])
   else
    newButton(1, y2 +1, files[n],"<DIRECT>", "redirect", path.. "/" ..files[n])
   end
  else
   newButton(1, y2 +1, files[n], "<LUAFLE>", path.. "/" ..files[n], path.. "/" ..files[n])
  end
end
installations = #files +3
end
function reDraw(derp)
term.clear()
term.setCursorPos(1,1)
drawScreen()
if not running == "" then
  term.clear()
  term.setCursorPos(1,1)
  shell.run(running)
  run = false
end
if currentY > installations then
  currentY = installations
end
term.setCursorPos(currentX, currentY)
write(Cursor)
end
while true do
reDraw(running)
if run == false then
  break
elseif run == true then
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then --up
   if currentY > 3 then
    currentY = currentY -1
   end
   reDraw(running)
  elseif key == 31 or key == 208 then --down
   if currentY < installations then
    currentY = currentY +1
   else
    currentY = installations
   end
   reDraw(running)
  elseif key == 203 or key == 30 then --left
   --currentX = currentX -1
   reDraw(running)
  elseif key == 205 or key == 32 then --right
   --currentX = currentX +1
   reDraw(running)
  elseif key == 28 then
   pressed = true;
   reDraw(running)
   sleep(0.0001)
   pressed = false;
  end
end
end
MysticT #2
Posted 02 April 2012 - 02:48 AM
If you are getting the error on the title, it's because you have a recursive function wich calls itself over and over, causing a stack overflow.
If you get another error, post the error message so we can help.
You should really cleanup a little the code, it has a lot of little errors, function parameters never used, repeated variable names, etc. It makes the code hard to follow, and it makes it more error prone.