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

Code causes java.lang.ArrayIndexOutOfBoundsException?

Started by Slappy826, 15 June 2014 - 08:08 PM
Slappy826 #1
Posted 15 June 2014 - 10:08 PM
CC Versions: 1.33 (Tekkit)

I cant seem to find where something might be out of bounds, might it be rednet?


rednet.open("top")
local id, msg = rednet.receive()
print("msg: "..msg)
if msg:lower():find("true") then
print(msg:lower():find("true"))
redstone.setOutput("right", true)
shell.run("lights")
end
if msg:lower():find("false") then
print(msg:lower():find("false"))
redstone.setOutput("right", false)
shell.run("lights")
end
shell.run("lights")
Lignum #2
Posted 15 June 2014 - 10:41 PM
Please properly format your code the next time you ask a question, because it helps identify the problem and more people will try to help. You can use this tool, which does it for you but it's better to do so while programming.

I just quickly formatted it for future use:

rednet.open("top")

local id, msg = rednet.receive()
print("msg: "..msg)

if msg:lower():find("true") then
   print(msg:lower():find("true"))
   redstone.setOutput("right", true)
   shell.run("lights")
end

if msg:lower():find("false") then
   print(msg:lower():find("false"))
   redstone.setOutput("right", false)
   shell.run("lights")
end

shell.run("lights")

Anyway, is shell.run("lights") used to restart the program? Because there's a high chance it causes a stack overflow (the ArrayIndexOutOfBoundsException you're seeing) by creating infinite recursion. If so, you should use a while loop instead.
TheOddByte #3
Posted 15 June 2014 - 10:43 PM
It might be this part that causes that problem

msg:lower():find("true")
Try doing this

rednet.open( "top" )
local id, msg = rednet.receive()
msg = msg:lower() -- Lower it here
if msg:find( "true" ) then
	print( msg:find( "true" )
	redstone.setOutput( "right", true )
	shell.run( "lights" )

elseif msg:find( "false" ) then -- Use elseif instead
	print( msg:find( "false" ) )
	redstone.setOutput( "right", false )
	shell.run( "lights" )
end
shell.run( "lights" )
But if I remember correctly that error usually occurs when you call some function recursively

local function foo()
	print( "bar" )
	foo()
end

Edit: Damn ninjas! .-.
Edited on 15 June 2014 - 08:43 PM