295 posts
Location
In the TARDIS at an unknown place in time.
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.
1604 posts
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
105 posts
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.
295 posts
Location
In the TARDIS at an unknown place in time.
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?
105 posts
Posted 14 October 2012 - 02:56 AM
Correct.
295 posts
Location
In the TARDIS at an unknown place in time.
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.
105 posts
Posted 14 October 2012 - 07:48 AM
What does your code look like now?
295 posts
Location
In the TARDIS at an unknown place in time.
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.
2088 posts
Location
South Africa
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 :)/>/>