Jump to content




"Small" Problem


  • You cannot reply to this topic
8 replies to this topic

#1 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 14 October 2012 - 01: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.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 14 October 2012 - 01: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


#3 Fatal_Exception

  • New Members
  • 105 posts

Posted 14 October 2012 - 01:35 AM

That's caused by a stack overflow, because you're calling your function from itself repeatedly (infinite recursion). Use a loop instead.

#4 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 14 October 2012 - 01:53 AM

View PostMysticT, on 14 October 2012 - 01:34 AM, said:

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?

#5 Fatal_Exception

  • New Members
  • 105 posts

Posted 14 October 2012 - 01:56 AM

Correct.

#6 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 14 October 2012 - 04: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.

#7 Fatal_Exception

  • New Members
  • 105 posts

Posted 14 October 2012 - 06:48 AM

What does your code look like now?

#8 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 14 October 2012 - 06:58 AM

View PostFatal_Exception, on 14 October 2012 - 06:48 AM, said:

What does your code look like now?
I believe I fixed it. I accidentally forgot to remove calling the function.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 14 October 2012 - 10:30 AM

View Postbrett122798, on 14 October 2012 - 06:58 AM, said:

View PostFatal_Exception, on 14 October 2012 - 06:48 AM, said:

What does your code look like now?
I believe I fixed it. I accidentally forgot to remove calling the function.

Silly, Silly mistake :)/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users