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

"Small" Problem

Started by brett122798, 14 October 2012 - 12:07 AM
brett122798 #1
Posted 14 October 2012 - 02:07 AM
Okay, I made a test program of an my soon to be EU measuring program. However, after a about 3 minutes, I get this error message:


java.lang.ArrayIndexOutOfBoundsException


I don't know why this happens or what the fix is. I think it may be because it constantly sends data, but I'm not sure. Here's the code:



rednet.open("left")

data = "0"
test = 0

function main()
if redstone.getInput("front") == true then
data = "1"
test = test + 1
end
if data == "1" then
rednet.send(15, data)
end
term.clear()
term.setCursorPos(1, 1)
print(test)
data = "0"
sleep(1)
main()
end

main()

Please remember, this code is very sloppy because I put it together in a few minutes.
MysticT #2
Posted 14 October 2012 - 02:34 AM
Don't use recursive functions (function that calls itself) to make loops, use a proper loop instead (like while or for, depending on the situation):

rednet.open("left")

local data = "0"
local test = 0

while true do -- infinite loop
  if redstone.getInput("front") then
    data = "1"
    test = test + 1
  end
  if data == "1" then
    rednet.send(15, data)
  end
  term.clear()
  term.setCursorPos(1, 1)
  print(test)
  data = "0"
  sleep(1)
end
Fatal_Exception #3
Posted 14 October 2012 - 02:35 AM
That's caused by a stack overflow, because you're calling your function from itself repeatedly (infinite recursion). Use a loop instead.
brett122798 #4
Posted 14 October 2012 - 02:53 AM
Don't use recursive functions (function that calls itself) to make loops, use a proper loop instead (like while or for, depending on the situation):

rednet.open("left")

local data = "0"
local test = 0

while true do -- infinite loop
  if redstone.getInput("front") then
	data = "1"
	test = test + 1
  end
  if data == "1" then
	rednet.send(15, data)
  end
  term.clear()
  term.setCursorPos(1, 1)
  print(test)
  data = "0"
  sleep(1)
end
Oh, ok, thanks. Btw, "while true do" 's can be got out of by "break", correct?
Fatal_Exception #5
Posted 14 October 2012 - 02:56 AM
Correct.
brett122798 #6
Posted 14 October 2012 - 05:47 AM
Guys, I'm still getting the same error even with changing to a loop.


Sorry I took so long to come back and say that, I was at the movies.
Fatal_Exception #7
Posted 14 October 2012 - 07:48 AM
What does your code look like now?
brett122798 #8
Posted 14 October 2012 - 07:58 AM
What does your code look like now?
I believe I fixed it. I accidentally forgot to remove calling the function.
remiX #9
Posted 14 October 2012 - 11:30 AM
What does your code look like now?
I believe I fixed it. I accidentally forgot to remove calling the function.

Silly, Silly mistake :)/>/>