←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Turtle throws [Error] java.lang.ArrayIndex...

Aracan's Photo Aracan 25 Mar 2013

Hi Folks !

I've thought i have written a good program for my turtle friends to mine with Mining wells, but after a short time the first turtle throws me a [Error]
bios:140 vm error: java.lang.ArrayIndexOutOfBoundsException: 256

After looking this up in google it seems i have a recursion in my programm, bit i do not find where. perhaps you can help me.
And please dont lough at my Amateur code >.< . Im still in my beginnings.

Programm 1: to receive a message form the Main Computer to start the whole operation

rednet.open("right")
senderId, message, distance= rednet.receive()
  term.setCursorPos(1,3)
  term.write("warte auf nachricht")
  if message== "place" then
  term.write("habe place erhalten")
  shell.run("mining")
  end
rednet.close("right")

Programm 2: to place the Mining Well wait for time (x) clear the inventory, than dig the MiningWell

function tPlace()
    turtle.select(1)
	turtle.place()
end

function cleanInv()
  for i=1, 16 do
	turtle.select(i)
    turtle.dropDown()
  end

end
			  
function digWell()
  for i=1, 16 do
    if turtle.getItemCount(i) ==0 then
	  turtle.select(1)
	  turtle.dig()
  
   else
	 cleanInv()							  
   end
 end
end


  term.clear()
  term.setCursorPos(1,1)
  term.write("ich arbeite")

  tPlace()
  sleep(6,5)
  cleanInv()
  digWell()
  shell.run("fertig")
  shell.run("test")


Programm 3: to send "i am finish" back to the Main Computer

rednet.open("right")
rednet.send(47,"01")
term.setCursorPos(1,2)
term.write("habe fertig gesendet")
rednet.close("right")

so these 3 Programs run on my turtles. They do work, but like i said, only for about .. 5-10 minutes , and than it throws the Error.
I hope you guys can help me .
Quote

Doyle3694's Photo Doyle3694 25 Mar 2013

What is fertig and test?
Quote

Doyle3694's Photo Doyle3694 25 Mar 2013

You know what, I know the problem now, program 3 will run done before program 1, it process in this order:
rednet.open("right")
senderId, message, distance= rednet.receive()
  term.setCursorPos(1,3)
  term.write("warte auf nachricht")
  if message== "place" then
   term.write("habe place erhalten")
   rednet.open("right")
   rednet.send(47,"01")
   term.setCursorPos(1,2)
   term.write("habe fertig gesendet")
   rednet.close("right")
  end
rednet.close("right")

you are trying to close rednet when it is not open, you can remove both rednet.open() and rednet.close() in program 3
Quote

Aracan's Photo Aracan 25 Mar 2013

Mhm .... okay. That makes sense.
I will try that out tomorrow !
But thank you very very much !

PS: While i was waiting for someone to answer this post, i solved the problem by rebooting the turtles in Programm to after
shell.run("fertig")

then i moved
shell.run("test")
in the startup program so that it will start after rebooting.

That solved the problem ... but i think its not a "clean" solution. So i will defenitly try your tipp with deleting the rednet open and close.

again, thank you very much :)
Quote

Doyle3694's Photo Doyle3694 25 Mar 2013

So the problem lies in fertig and test? Could you show us the code for both of those?
Quote

theoriginalbit's Photo theoriginalbit 25 Mar 2013

View PostAracan, on 25 March 2013 - 11:40 AM, said:

While i was waiting for someone to answer this post, i solved the problem by rebooting the turtles in Programm to after
shell.run("fertig")
It sounds like you have something nasty in fertig or test... the reason rebooting worked is because it clears the stack (which is whats causing the java.lang.ArrayIndexOutOfBoundsException, which is a stack overflow)
Quote

Aracan's Photo Aracan 25 Mar 2013

Sure, i will show you the code if i get it working without the error.
I must go to work for now, but after that i will try deleting the rednet.open and rednet close in "test" and "fertig" .... i dont think i must open and close the rednet every time.
And i should always do my name in english now. It looks incredibly stupid :D
Quote

Aracan's Photo Aracan 26 Mar 2013

Hey There !
Its me again * yay *
Im on my Server right now and wanted to test the changes we have discussed , but our Power System is currently out of order ....

So i've been wondering. If i am right the Error i had get was because of "recursion" that means to me, program "a" calls itself every time.
Example

-- Programm 1 :
function print1()
print("1)"
shell.run("Programm2")
end
--Programm2:
function print2()
print("2")
shell.run("Programm1")
end

That is recursion for me. Or i have misunderstand it.
Anyways. Perhaps i get the error because the programs calling itself again and again ... ??

If thats the point , can a solution look like these ?

--Main Programm  like Startup
while true do
shell.run("Programm1")
shell.run("Programm2")
shell.run("Programm3")
sleep(1)
end

I think i am going mad ... since i started working with computercraft the only thing i can think of is code code code ...


by the way, i have made the changes to one turtle .. and i am waiting that our Power Supply runs again. And then i will test it out immediately
Quote

faubiguy's Photo faubiguy 26 Mar 2013

Yes, if the program (or function) calls itself again and again, or calls other programs that do, that's recursion, and it will cause an error if it gets too far. Putting the shell.run calls in a while loop like you posted and having the programs not run any other programs themselves should fix the stack overflow error.
Quote

Aracan's Photo Aracan 26 Mar 2013

Cool , okay ... that pleases me :)
Okay, than I'm gonna do, what i have to do ... I'm gonna write that code ... :D

Thank you very much for your help erverybody ! 1000 kisses and hugs. And yes, I am happy :)
Quote