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

[Error] Receiving java.lang.ArrayIndexOutOfBoundsException: 256 in Console

Started by Rotaretilbo, 20 July 2012 - 08:54 PM
Rotaretilbo #1
Posted 20 July 2012 - 10:54 PM
I was tasked yesterday with writing a simply clock program for a town nearby where I live in an SMP server. The constraints were simply to have the in-game time write to a monitor in the center of town which would automatically refresh itself and was thus self-sufficient. With a little research, I had all the resources necessary to write such a program. However, about ten minutes later, I was called back to the town, as the clock had stopped refreshing. When I opened up the Console, it showed that the program had crashed, citing some obscure out of bounds exception. Given that this was an actual java error (commonly associated with Minecraft actually crashing), I was unable to find any articles or threads citing the same issue. Confused, I tweaked the program a few times, but kept receiving the error, typically being asked to come back out five to ten minutes later. Frustrated (mostly because I live in the midst of a jungle some three hundred blocks from this town), I went into my single-player test world and tried to replicate the issue. As a perfectionist, I took the time to properly center the clock in the screen, and also tweaked the delay between refreshes so that the clock ran smoothly. In this, I noticed a significantly shorter time between starting the program and it crashing. Here is the error message and initial code:

Error Message on Run via Startup
CraftOS 1.3
bios:15: vm error:
java.lang.ArrayIndexOutOfBoundsException: 256
>

Error Message on Manually Run
CraftOS 1.3
> clock
java.lang.ArrayIndexOutOfBoundsException
>

Test Code Mk.I
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
sleep(0.1)
time()
end
time()

Curious about the nature of the error, I logged the start and end time four times. The times were as follows:

Original Test Run
12:07 - 12:36
13:02 - 13:32
13:43 - 14:12
14:20 - 14:49

I noted here that the program was consistently crashing after between 29 and 30 in-game minutes. Curious, I decided to mess with the one major component from the SMP program I'd written that I had changed: the sleep function.

Test Code Mk.II
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
sleep(0.5)
time()
end
time()

Second Test Run
15:12 - 17:53
18:04 - 20:45
21:08 - 23:49
0:00 - 2:40

Now I was getting somewhere. The program crashed roughly between 160 and 161 in-game minutes. It wasn't quite directly inverted, but it took almost exactly four times longer to crash when I increased the sleep by five times. Curious about this relationship, I ran another test.

Test Code Mk.III
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
sleep(1)
time()
end
time()

Third Test Run
3:04 - 7:56
8:30 - 13:21
13:33 - 18:25
19:20 - 0:11

In this test, the program consistently after 291 to 292 in-game minutes. I had one more thing I wanted to test. I removed the sleep function altogether, just to see what would happen.

Test Code Mk.IV
mon = peripheral.wrap("back")
mon.setTextScale(2)
orig = textutils.formatTime(os.time(), true)
function time()
mon.clear()
mon.setCursorPos(11,4)
mon.write(orig)
mon.setCursorPos(11,6)
mon.write(textutils.formatTime(os.time(), true))
time()
end
time()

Fourth Test Run
0:42 - 0:42

The program crashed immediately. Oddly, this was the first instance where any part of the error message changed:

New Error Message on Run via Startup
CraftOS 1.3
bios:5: vm error:
java.lang.ArrayIndexOutOfBoundsException: 256
>

So, obviously, I can work around this problem by just indefinitely extending the time the computer spends asleep between resets, but this is hardly ideal, given that this is supposed to be a live clock. Thus, I thought that perhaps, with all this information, someone here who knows ComputerCraft and/or lua better than I might be able to suggest why I am encountering this error (such as some syntax formatting quirk) or a possible solution that does not involve ramping the sleep up past 1 second. Hope this information is sufficient.
Rotaretilbo #2
Posted 20 July 2012 - 10:57 PM
Oh, I should probably add that, yes, I did try using the raw os.time() rather than using the textutils.formatTime function, and no, that did not affect the outcome whatsoever. I chose to cite using the formatted time here simply because the results are a lot easier to read.
Grim Reaper #3
Posted 20 July 2012 - 10:58 PM
In your first test program you create an infinite loop via recursion which could be causing the problem. If other programs are working on the server then your program is obviously at fault and not the server.

Try creating an infinite loop via other means like a 'while true do'.
Rotaretilbo #4
Posted 20 July 2012 - 11:08 PM
I'll blame the tutorials on the ComputerCraft wiki for teaching me to use recursion for infinite loops. I suppose in past programs, I've not been using programs that have the capacity to loop so many times in such a short period of time. Initial tests using a do-while infinite loop appear to be successful. Can't believe it didn't occur to me to try using something other than recursion. Many thanks.
Lyqyd #5
Posted 21 July 2012 - 12:55 AM
I'll blame the tutorials on the ComputerCraft wiki for teaching me to use recursion for infinite loops. I suppose in past programs, I've not been using programs that have the capacity to loop so many times in such a short period of time. Initial tests using a do-while infinite loop appear to be successful. Can't believe it didn't occur to me to try using something other than recursion. Many thanks.

Which tutorial(s), specifically? I'd like to correct them if this is the case.
Rotaretilbo #6
Posted 22 July 2012 - 12:51 AM
The first of two tutorials in Making a Password Protected Door. My very first foray into ComputerCraft was doing just that, and so I assumed recursion would be an acceptable way of creating an infinite loop for a program.

I gave it some thought, and I imagine what is happening is that the program creates an Array with each instance of recursion, where the limit is 256. Thus, once the function runs for the 257th time, it crashes the program and throws the error. In the future, if I choose to use recursion, I'll probably include some kind of counter to reboot the computer at 250 runs or something like that.
Lyqyd #7
Posted 22 July 2012 - 01:42 AM
The first of two tutorials in Making a Password Protected Door. My very first foray into ComputerCraft was doing just that, and so I assumed recursion would be an acceptable way of creating an infinite loop for a program.

I gave it some thought, and I imagine what is happening is that the program creates an Array with each instance of recursion, where the limit is 256. Thus, once the function runs for the 257th time, it crashes the program and throws the error. In the future, if I choose to use recursion, I'll probably include some kind of counter to reboot the computer at 250 runs or something like that.

Okay, I've fixed it. I didn't add new screenshots, but I removed the offending ones and re-wrote the tutorial in question. Thanks for pointing it out!