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

Java Array Out Of Bounds Exception

Started by DesertEagle-2000, 20 May 2016 - 01:13 PM
DesertEagle-2000 #1
Posted 20 May 2016 - 03:13 PM
So, I just released my first game yesterday, and it has an error that, if you play it for a long time, it'll give a Java Array Out Of Bounds Exception. I have looked on the internet for solutions, and what I found was a Garbage Collector. My guestion is, does it help prevent the error and if not, what would. I really need a fix because all my creations in ComputerCraft have this error. Thanks if you want to help me :)/>!
KingofGamesYami #2
Posted 20 May 2016 - 03:15 PM
When I google it, I get pretty helpful results. It's your program at fault, not java.
Edited on 20 May 2016 - 01:16 PM
Lyqyd #3
Posted 20 May 2016 - 03:15 PM
Please post the code that's throwing the error.
Dragon53535 #4
Posted 20 May 2016 - 03:22 PM
You're calling a function over and over without letting any end. Recursion. As Lyqyd said, show your code and we can point out what went wrong, usually it's because you had a main function call another function that did a certain thing, that then called the main function again.
DesertEagle-2000 #5
Posted 20 May 2016 - 03:26 PM
When I google it, I get pretty helpful results. It's your program at fault, not java.
Yeah I know It's my program's fault. I just don't know how to fix that :(/>

Please post the code that's throwing the error.

Well, it's not really one part of the code that gives the error. At random times, while playing the game, it gives that error. But you can find the game here
Dragon53535 #6
Posted 20 May 2016 - 03:46 PM
You haven't indented your code…

I'm just going to ask, what function/where is the starting point of your code that actually runs the game itself. What part do you go back to for when you create a game.
DesertEagle-2000 #7
Posted 20 May 2016 - 03:51 PM
what function/where is the starting point of your code that actually runs the game itself.

Oh that. Then, I think it's this function:
function begin()

gameutils.initializeBuffer()

drawGUI()

local count = 1

repeat

table.remove(CardsP2Images,count)
table.insert(CardsP2Images,count,backside)

count = count + 1

sleep(0.35)

drawGUI()

until count==6

CardsRevealP2 = false

sleep(0.15)

local count = 1

repeat

table.remove(CardsP1Images,count)
table.insert(CardsP1Images,count,backside)

count = count + 1

sleep(0.35)

drawGUI()

until count==6

sleep(0.3)

RandomizeCards()

end

Sorry guy's if I didn't get what you mean. I can be very dumb sometimes ;_;
Edited on 20 May 2016 - 02:19 PM
KingofGamesYami #8
Posted 20 May 2016 - 04:23 PM
I've been looking through your code to see if any of your functions are calling each other, and I don't see it happening.

In general, look for any circumstance where a function might call a function which calls the first function. Eg:


function a()
  b()
end

function b()
  a()
end
DesertEagle-2000 #9
Posted 20 May 2016 - 04:31 PM
look for any circumstance where a function might call a function which calls the first function.

Got it! Thanks for looking into it!
Edited on 20 May 2016 - 02:32 PM
Dragon53535 #10
Posted 20 May 2016 - 04:59 PM
At any stage, is any function anywhere calling begin()?
DesertEagle-2000 #11
Posted 20 May 2016 - 07:20 PM
At any stage, is any function anywhere calling begin()?

Nope. Not in a function. Just at the very bottom of the program. (That's why it's called begin ;)/> )
Dragon53535 #12
Posted 20 May 2016 - 07:52 PM
Alright then, is any other function calling RandomizeCards()
Bomb Bloke #13
Posted 21 May 2016 - 02:03 AM
As I already pointed out in the script's thread, you're getting the error because you have the script re-run itself over and over. Each running instance of the script is another pile of functions loaded onto the function stack. Guess how many entries LuaJ lets you squeeze in there?

You must keep in mind that whenever one function calls another, the first one isn't unloaded - it doesn't magically shut down, it stays in RAM and waits for the latter to return. shell.run() isn't exempt from this rule.

If you're still not getting it, try using your quit button after playing a few hands. You'll find you get black screens you have to press past, and then to actually get back to CraftOS, you'll need to press quit over and over again… once for each instance of the script you're running!

-- This is a psuedo-code example of your recursive loop.

if not userTriesToQuit then shell.run("thisScript") end

-- User will have to try to quit once for each time this loop repeats,
-- assuming the function stack doesn't blow up first!

-- This is a psuedo-code example of a proper loop.

while true do
  if userTriesToQuit then break end
end

-- User will have to try to quit once no matter how many times this loop repeats.
-- Each iteration leaves the function stack alone.
Edited on 21 May 2016 - 12:34 AM
DesertEagle-2000 #14
Posted 21 May 2016 - 09:51 AM
Ok I understand that these function's stack up in memory and at some point give an error and you have to click guit more then once. Now my only question now is, is there a way to forget those functions so they don't stack up in memory?
Waitdev_ #15
Posted 21 May 2016 - 11:46 AM
I don't really understand what's going on to make it break in your code, but i think the way you can avoid stack up in memory is don't have a table too big (possibly)?
also whenever you've got a loop, always have a sleep() function otherwise it might cause the error.
Dragon53535 #16
Posted 21 May 2016 - 11:59 AM
What it sounds like is you're using shell.run on your program over and over again. Perhaps you could just return all the way down to the beginning.

The only way to make the program "forget" those functions, is to finish all the ones above them, and have it run to the end of the function.


local foo,bar
foo = function()
  write("Please enter word: ")
  local c = read()
  if c == "word" then
    print("We out bois!")   
    return
  else
    bar()
  end
end
bar = function()
  foo()
  print("wooo")
end
bar()
Eventually this code will cause a stack overflow, the error you got above, however if you type the word 'word', then it won't and will print out we out bois and wooo until it falls to the bottom of the stack.
KingofGamesYami #17
Posted 21 May 2016 - 02:06 PM
To avoid problems use proper tail recursion.


local foo --#pre declare foo
function foo( i ) --#make foo a function
  print( i ) --#print the value given to foo
  sleep( 1 ) --#wait 1 second
  return foo( i + 1 ) --#end the function (return) then call foo with the value + 1
end
foo( 0 ) --#start foo

This code will not have any issues, because it is ending the previous function before calling the next one. However, typically the best way to avoid this error is to simply avoid recursion and use a loop instead.
Bomb Bloke #18
Posted 21 May 2016 - 02:24 PM
To avoid problems use proper tail recursion.

That'd work if shell.run() used tail calls. And technically it does, but it calls functions - plural - which don't: meaning tail calls aren't the solution here. They'll delay the error, not prevent it.