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

The least helpful debug error ever

Started by TheRockettek, 15 April 2016 - 07:15 PM
TheRockettek #1
Posted 15 April 2016 - 09:15 PM
So i tried doing my program and when i run it, the computer just errors java.lang.ArrayIndexOutOfBoundException

Heres the program:


monitor = peripheral.wrap("right")
tz = "UTC"

function getTime(tz,format)
 time = http.get("http://therockettek.hol.es/API/Time.php?  TZ="..textutils.urlEncode(tz).."&FORMAT="..textutils.urlEncode(format)).readAll()
 if time == "" then
  time = "ERR"
 end
 return time
end

function drawBox()
 hour = getTime(tz,"G")
 if hour > 16 and hour < 5 then
   print("night")
   bx,by = monitor.getSize()
   paintutils.drawFilledBox(1,1,bx,by,colors.black) 
 else
  print("day")
  bx,by = monitor.getSize()
  paintutils.drawFilledBox(1,1,bx,by,colors.yellow)
 end
end

drawBox()
Edited on 15 April 2016 - 07:16 PM
TheRockettek #2
Posted 15 April 2016 - 09:21 PM
Anavrins #3
Posted 15 April 2016 - 09:32 PM
My first guess is that getTime returns a string, and you're trying to compare it with a number.
Try replacing return time into return tonumber(time)
There's also a space between php? and TZ, which might make it always error.
Edited on 15 April 2016 - 07:35 PM
Bomb Bloke #4
Posted 16 April 2016 - 12:25 AM
The "least helpful" errors are the ones where you get nothing at all, or worse, when the script doesn't even crash…

This particular error generally shows up when you make an infinite loop of recursive function calls. Each instance of a running function goes into a Java array, and once you max out its capacity you get a crash.

However, the script you've posted doesn't do that. In fact, running it myself I get an entirely different error, based on you trying to compare a number against a string. I suspect you're not running the script you think you're running.

Another couple of points, for whatever reason attempting to get and read from a URL in one go never seems to work, and you should be retaining the handle so you can close it anyway:

local handle = http.get("http://therockettek.hol.es/API/Time.php?TZ="..textutils.urlEncode(tz).."&FORMAT="..textutils.urlEncode(format))
local time = handle.readAll()
handle.close()

… and don't forget to redirect to your wrapped monitor!