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

Error on program on my server, out of bounds exception for an array?

Started by DannySMc, 17 July 2014 - 09:34 PM
DannySMc #1
Posted 17 July 2014 - 11:34 PM
Okay so I am making a simple file server. Here is the code:

-- File/Database Server
-- Created By DannySMc
nVersion = 1
--[[ Commands (sCommandName (arguments) -> Returns -> Overview)
"exists" (strName) -> sTrue or sFalse -> Will check if file exists and return either true of false
"download" (strName) -> sSerializedFile or sFalse -> Will download the file if it exists
"upload" (strName) (tSerializedFile) -> sTrue or sError -> Will upload file to server, returning true or the error
"checkUsername" (strUsername) -> sTrue or sFalse -> Will check username with user database and reply with true or false
"postUsername" (strUsername) -> sTrue or sFalse -> Add username to database
]]
-- Peripheral Functions
function findPeripheral(Perihp) --Returns side of first matching peripheral matching passed string
  for _,s in ipairs(rs.getSides()) do
	if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
	  return s  
	end
  end
  return false
end
-- Common Draw Functions
function cs()
term.clear()
term.setCursorPos(1,1)
return
end
function setCol(textColour, backgroundColour)
if textColour and backgroundColour then
  if term.isColour() then
   term.setTextColour(colours[textColour])
   term.setBackgroundColour(colours[backgroundColour])
   return true
  else
   return false
  end
else
  return false
end
end

function resetCol()
if term.isColour then
  term.setTextColour(colours.white)
  term.setBackgroundColour(colours.black)
  return true
else
  return false
end
end
-- Print Functions
function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
  local x, y = term.getSize()
  x = x/2 - #Text/2
  term.setCursorPos(x, Line)
  if Color then setCol(Color, BkgColor) end
  term.write(Text)
  if NextLine then
	term.setCursorPos(1, NextLine)
  end
  if Color then resetCol(Color, BkgColor) end
  return true
end
function printL(Text, Line, NextLine, Color, BkgColor) -- print line

  local x, y = term.getSize()
  if ((term.isColor) and (term.isColor() == false) and (Text == " ")) then Text = "-" end
  for i = 1, x do
	term.setCursorPos(i, Line)
	if Color then setCol(Color, BkgColor) end
	term.write(Text)
  end
  if NextLine then
	term.setCursorPos(1, NextLine)
  end
  if Color then resetCol(Color, BkgColor) end
  return true
end
function printA(Text, xx, yy, NextLine, Color, BkgColor) -- print anywhere
  term.setCursorPos(xx,yy)
  if Color then setCol(Color, BkgColor) end
  term.write(Text)
  if NextLine then
	term.setCursorPos(1, NextLine)
  end
  if Color then resetCol(Color, BkgColor) end
  return true
end
function clearLine(Line, NextLine) -- May seem a bit odd, but it may be usefull sometimes
  local x, y = term.getSize()
  for i = 1, x do
	term.setCursorPos(i, Line)
	term.write(" ")
  end
  if not NextLine then
	x, y = term.getCursorPos()
	term.setCursorPos(1, y+1)
  end
  return true
end
function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
  local x, y = term.getSize()
  if Color then setCol(Color, BkgColor) end
  if not Text then Text = "*" end
  lengthX = lengthX - 1
  lengthY = lengthY - 1
  EndX = StartX + lengthX
  EndY = StartY + lengthY
  term.setCursorPos(StartX, StartY)
  term.write(string.rep(Text, lengthX))
  term.setCursorPos(StartX, EndY)
  term.write(string.rep(Text, lengthX))
  for i = StartY, EndY do
	term.setCursorPos(StartX, i)
	term.write(Text)
	term.setCursorPos(EndX, i)  
	term.write(Text)
  end
  resetCol(Color, BkgColor)
  return true
end

-- Download Functions
function getPBFile(PBCode, uPath) -- pastebin code of the file, and path to save
local PBFile = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(PBCode))
if PBFile then
  local PBfileToWrite = PBfile.readAll()
  PBfile.close()
  local file = fs.open( uPath, "w" )
  file.write(PBfileToWrite)
  file.close()
   return true
  else
   return false
  end
end
-- Database Functions
db = {}
db.__index = db
function db.delete(Filename)
  if fs.exists(Filename) then
	fs.delete(Filename)
	return true
  end
  return false
end
function db.load(Filename)
  if not fs.exists(Filename) then
	local F = fs.open(Filename, "w")
	F.write("{}")
	F.close()
  end
  local F = fs.open(Filename, "r")
  local Data = F.readAll()
  F.close()
  Data = textutils.unserialize(Data)
  return Data
end
function db.save(Filename, ATable)
  local Data = textutils.serialize(ATable)
  local F = fs.open(Filename, "w")
  F.write(Data)
  F.close()
  return true
end
function db.search(searchstring, ATable)
  for i, V in pairs(ATable) do
	if tostring(ATable[i]) == tostring(searchstring) then
	  return i
	end
  end
  return 0
end
function db.removeString(Filename, AString)
  local TempT = db.load(Filename)
  if type(TempT) ~= "table" then return false end
  local Pos = db.search(AString, TempT)
  if Pos > 0 then
	table.remove(TempT, Pos)
	db.save(Filename, TempT)
	return true
  else
	return false
  end
end
function db.insertString(Filename, AString)
  local TempT = db.load(Filename)
  if type(TempT) ~= "table" then TempT = {} end
  table.insert(TempT, AString)
  db.save(Filename, TempT)
  return true
end

-- Vars
tc = "white" -- text colour
bc = "cyan" -- background colour
rednet.open("back") -- Allows wireless control
nCSID = 14 -- The command server identification number
nServerID = os.getComputerID()
nUsersAdded = 0
nUserChecks = 0
nDownloads = 0
nUploads = 0
nPingCount = 0
local function fPing(nID)
rednet.send(nID, "online")
end
local function exists(nID, sFile)
if fs.exists("files/"..sFile) then
  rednet.send(nID, "sTrue")
else
  rednet.send(nID, "sFalse")
end
end
local function upload(nID, sFileName, sFileContents)
local f = fs.open("files/"..sFileName, "w")
tFileContents = textutils.unserialize(sFileContents)
f:write(tFileContents)
f:close()
exists(nID, sFileName)
end
local function download(nID, sFile)
if fs.exists(sFile) then
  local tData = fs.open("files/"..sFile, "r")
  sData = textutils.serialize(tData)
  rednet.send(nID, sData)
else
  rednet.send(nID, "File Doesn't Exist")
end
end
local function userDB(sMode, nID, sUsername) --Modes: Add user: "add", check user: "check"
if sMode == "add" then
  db.insertString("UsersDB", sUsername)
  local a = db.load("UsersDB")
  if db.search(sUSername, a) then
   rednet.send(nID, "sTrue")
  else
   rednet.send(nID, "sFalse")
  end
elseif sMode == "check" then
  local a = db.load("UsersDB")
  if db.search(sUSername, a) then
   rednet.send(nID, "sTrue")
  else
   rednet.send(nID, "sFalse")
  end
end
end
local function drawTime()
local nTime = textutils.formatTime(os.time())
printA("Time: ", 43, 1, false, tc, bc)
os.startTimer(1)
end
local function showStats()
-- Draw stats
term.setCursorPos(1, 4)
print("> Pings: "..nPingCount)
print("> Uploads: "..nUploads)
print("> Downloads: "..nDownloads)
print("> User Checks: "..nUserChecks)
print("> "..nUsersAdded.." Users Added")
end
local function main()
-- Draw Screen
drawBox(1, 51, 1, 2, " ", tc, bc)
printA("File Server - Version: 1", 1, 1, false, tc, bc)
drawTime()
drawBox(1, 51, 19, 1, " ", tc, bc)
printC(">> Created By DannySMc <<", 19, false, tc, bc)
term.setCursorPos(1, 10)
print("> The File Server: Online")
print("> File Server ID: "..nServerID)

while true do
  showStats()
  local args = {os.pullEvent()}
  if args[1] == "timer" then
   drawTime()
  elseif  args[1] == "rednet_message" then
   if args[3] == "ping" then
	fPing(args[2])
   elseif args[3] == "upload" then
	local arg1, sFileName = rednet.receive(2)
	local arg1, sFileContents = rednet.receive(2)
	upload(arg1, sFileName, sFileContents)  
   elseif args[3] == "download" then
	local nID, sFileName = rednet.receive(2)
	download(nID, sFileName)
   elseif args[3] == "checkUsername" then
	local nID, sUsername = rednet.receive(2)
	UserDB("check", nID, sUsername)
   elseif args[3] == "postUsername" then
	local nID, sUsername = rednet.receive(2)
	UserDB("add", nID, sUsername)
   end
  end
end
end
main()

and when the program tries to run, the computer block just goes black the screen goes black and it won't do anything, so I had a look into it and tried to edit the program, the computer block is "frozen" or "broken". So I had a look at the server log (self-hosted so the CMD displays information, and it said:


computercraft: Error running task.
java.lang.ThreadDeath
at java.lang.Thread.stop(Unknown Source)
at dan200.computercraft.core.computer.ComputerThread$1.run(ComputerThread.java:151)
at java.lang.Thread.stop(Unknown Source)

The server works still and all the other computers still work. Please help?:/

EDIT: The computer spat out this error: (It still has frozen and unusable)

nil: vm error:
java.lang.ArrayIndexOutOfBoundsException
Edited on 17 July 2014 - 09:37 PM
Bomb Bloke #2
Posted 18 July 2014 - 03:06 AM
Works for me. I can only suggest rebooting the server and making sure the code you've posted here is the code you're trying to run on the computer in concern.

Usually the symptoms you're seeing relate to an unyielding loop somewhere, but this code doesn't have that problem.
DannySMc #3
Posted 18 July 2014 - 11:06 AM
Works for me. I can only suggest rebooting the server and making sure the code you've posted here is the code you're trying to run on the computer in concern.

Usually the symptoms you're seeing relate to an unyielding loop somewhere, but this code doesn't have that problem.

It's connected to a LAN cable that connects to a computer that runs the "repeat" program…?
Bomb Bloke #4
Posted 19 July 2014 - 03:48 AM
Testing it under those extra conditions still doesn't trigger the issues you describe. I even added in a line to make it print "nTime", and so it just sits there counting out the seconds quite happily.