Jump to content




Can someone help fixing this error for me.


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

#1 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 05 April 2014 - 08:07 AM

every 30 to 50 min it crash with java.lang.ArrayIndexOutOfBoundsException. i know that it is function after 255 times it cant be called but i cant way to fix this probleem i have try other stuff but still it crashed.

this is code
http://pastebin.com/rR9JXSvy (This Is One i want to fix)

Github if you how to pull request.
Here

Edited by johnneijzen, 05 April 2014 - 08:11 AM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 April 2014 - 08:54 AM

Your problem is due to the fact that Lenght (typo btw, you spelt it wrong) invokes wide which then invokes Lenght again, etc. This is called cyclic recursion. It would require a bit of a rewrite but you can fix this with loops. However if you want the 'quick-and-dirty' method you can use tail calls to fix this. Basically add a 'return' before each of the calls and it'll fix your problem. I do suggest attempting to fix your code and use loops however, it promotes a better program design.

Some suggestions unrelated to the bug

I see you understand local variables, as you've used them in your program, I also suggest you make use of local functions. It will require a change of order so they work, and it will also require you to change your program to the suggestion of loops to fix your cyclic recursion problem.

Another suggestion I can make is to make use of the turtle_inventory event when waiting for more items from the user, instead of refreshing after 15 seconds.

#3 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 05 April 2014 - 09:09 AM

okey now understand my problem. thanks for tip about turtle_inventory event. :) i`m go start working on change code in 3 days because still staff to do.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 April 2014 - 09:15 AM

I've been ninja'd while typing, and BIT's covered all this already, but I've started so I'll finish.

Let's chop out most of the script to make it easier to read the flow:

Spoiler

As you've realised, the problem is that your script calls function after function after function... without ever allowing any of the previous functions to end. Eventually the built-up trail of calls crashes down.

You should be able to get around this easily by using tail calls - have each function attempt to return a theoretical result from the next. Doing this means that each new call replaces the previous one in the stack, saving it from filling up and overflowing.

The process basically involves determining where the last function call in each function is, and turning that into a return. Eg:

-- Mining Wide
function wide()
  ..
  if Wide == WC then
    ..
    return run()
  else
    return Lenght()
  end
end

It doesn't really matter whether the last function at the end of the trail returns anything. The point is that Lua now knows that since this function instance is only going to return whatever the next function does, it doesn't need to remember this function anymore.

However: Ideally you'd ditch the need for recursion, which again is made easy by having functions return data and by implementing a few while loops. Compare this to the paraphrased outline of your code I quoted above, and feel free to ask questions about whatever bits don't make sense to you:

Spoiler


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 April 2014 - 09:28 AM

View PostBomb Bloke, on 05 April 2014 - 09:15 AM, said:

if Lenght() then break end
if wide() then break end
taking a look i think the logic that better matches OP code would be
if Lenght() and wide() then break end


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 April 2014 - 09:33 AM

Nah. "or" might work, though.

#7 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 05 April 2014 - 09:51 AM

i have one question still what with 2 while true? can some one explain
Code:
while true do
while true do
if Lenght() then break end
if wide() then break end
end

if run() then break end
end

#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 April 2014 - 10:39 AM

  while true do                   -- Start a loop that repeats indefinitely.
    while true do                 -- Start another loop that repeats indefinitely.
      if Lenght() then break end  -- If the value returned by Lenght() is true, then jump out of the second loop.
      if wide() then break end    -- If the value returned by wide() is true, then jump out of the second loop.
    end                           -- This marks the end of the second loop.

    if run() then break end       -- If the value returned by run() is true, then jump out of the first loop.
  end                             -- This marks the end of the first loop.

So say we call the Lenght() function, and it finishes up by performing "return Wide == Wc". It checks to see if Wide is the same as Wc, and if so, the value it passes back is true; if not, the value will be false. We can hence use the called function as a value.

For example, this allows us to perform a true/false test by performing "if Lenght() then ...".

For more on loops, have a read through this.

Edited by Bomb Bloke, 05 April 2014 - 10:51 AM.


#9 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 05 April 2014 - 10:46 AM

okey.

#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 April 2014 - 10:51 AM

I kinda used some verbose coding to try to make the logic of the loops a little simpler for you, but that may've had the opposite effect. If it helps, this should do the same thing:

  repeat
    repeat until Lenght() or wide()
  until run()


#11 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 06 April 2014 - 05:27 PM

i spent about 2 day rewritten the program i think i`m almost done with programs
http://pastebin.com/rR9JXSvy

Edited by johnneijzen, 06 April 2014 - 07:40 PM.


#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 April 2014 - 01:08 AM

View Postjohnneijzen, on 06 April 2014 - 05:27 PM, said:

i spent about 2 day rewritten the program i think i`m almost done with programs
http://pastebin.com/rR9JXSvy
is there currently a problem, or are you just giving an update on progress?

so I got a little bored and wrote an example of using the turtle_inventory event, but also at the same time making the checking process easier for you, especially since I noticed on your GitHub that you use it in different programs. You can add more checks with one line of code (just add a new table entry), no need to go and write more if statements, no need for your recheck function, and other little things that you can read in the commented version
Code
Commented Code

Edited by theoriginalbit, 07 April 2014 - 01:10 AM.


#13 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 07 April 2014 - 04:51 PM

it was just giving an update on progress. it work like Charm after fixing some minor bug it mine fast as hell.

Posted Image

Edited by johnneijzen, 07 April 2014 - 06:13 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users