Jump to content




java.lang.ArrayIndexOutOfBoundsException

turtle java

9 replies to this topic

#1 EarthwyrmJim

  • Members
  • 6 posts

Posted 16 April 2013 - 12:31 PM

Alright, I'm working on a little program to scan all of my MFEs and total the EUs stored, then display the total and percentage total on a monitor sitting on top of the column of MFEs. I drew an image here. It will perform all actions perfectly, but when it reaches the top to print it just stops moving and hangs at the point where it should print. I've commented out the code that prints to the monitor to see if that was causing the issue, but it throws the same error: bios:124: vm error: java.lang.ArrayIndexOutOfBoundsException. Here is the code:

term.clear()
term.setCursorPos(1,1)
os.loadAPI("ocs/apis/sensor")
local functions = sensor.wrap("right")
local mfe
local total = 0
local count = 1
local mfeCount = 6
local maxStorage = 600000*mfeCount

local function recharge()
	local goto = count
	while count <= 7 do
		turtle.down()
		count = count + 1
	end
	sleep(5)
end

local function read()
	print("Scanning...")
	while count <= mfeCount do
		turtle.down()
		mfe = functions.getTargetDetails("0,0,1")
		if mfe.Stored > 600000 then
			total = total + 600000
		else
			total = total + mfe.Stored
		end
		count = count + 1
	end
end

local function print()
	if turtle.getFuelLevel() < 30 then
		recharge()
	end
	while count > 1 do
		turtle.up()
		count = count - 1
	end
	count = 0
	sleep(.5)
	--term.redirect(peripheral.wrap("front"))
	print(total)
	print(tostring(mfe.StoredPercentage).."%")
	--term.restore()
	total = 0
end
while true do
	read()
	print()
	sleep(.5)
end

I am using a sensor turtle with OpenCCsensors 0.1.4c. Any reason why this may be happening?

Thanks.

#2 SadKingBilly

  • Members
  • 160 posts

Posted 16 April 2013 - 12:37 PM

I'm pretty sure the problem is that you called one of your variable "goto". I'm not sure why it's causing a problem, because I didn't think goto was a reserved keyword in Lua, but I do know that Lua won't let me call a variable goto.

#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 16 April 2013 - 12:40 PM

First of all, when I get these problems that don't show the line in the my program I'll open the file and find the line.
In this case it is the os.pullEvent function that is causing your error.
function os.pullEvent( _sFilter )
local eventData = { os.pullEventRaw( _sFilter ) }
if eventData[1] == "terminate" then --Crashing at this line
  error( "Terminated", 0 )
end
return unpack( eventData )
end

I've had a look and can't find any pullEvents, so I'll keep looking.
Try to put a few 'print("Point 1")', point 2 etc. to see where it is occurring.

#4 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 16 April 2013 - 12:41 PM

 TheCoryKid, on 16 April 2013 - 12:37 PM, said:

I'm pretty sure the problem is that you called one of your variable "goto". I'm not sure why it's causing a problem, because I didn't think goto was a reserved keyword in Lua, but I do know that Lua won't let me call a variable goto.

I think you might be right there, that line isn't needed anyway. It's not used at all.

Also, it's not good practice to rewrite the print or read programs, you may want to consider changing there names to something like printPercentage.

#5 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 16 April 2013 - 12:49 PM

 oeed, on 16 April 2013 - 12:41 PM, said:

 TheCoryKid, on 16 April 2013 - 12:37 PM, said:

I'm pretty sure the problem is that you called one of your variable "goto". I'm not sure why it's causing a problem, because I didn't think goto was a reserved keyword in Lua, but I do know that Lua won't let me call a variable goto.

I think you might be right there, that line isn't needed anyway. It's not used at all.

Also, it's not good practice to rewrite the print or read programs, you may want to consider changing there names to something like printPercentage.
He is calling the function within itself. Look, he re-wrote the print() function and is calling it with intentions to print a message instead

local function print()
if turtle.getFuelLevel() < 30 then
recharge()
end
while count > 1 do
turtle.up()
count = count - 1
end
count = 0
sleep(.5)
print(total)
print(tostring(mfe.StoredPercentage).."%")
total = 0
end

#6 EarthwyrmJim

  • Members
  • 6 posts

Posted 16 April 2013 - 12:57 PM

That makes a ton of sense. I was scribbling pseudocode on a piece of paper and neglected to think about my choice in function names. As for the goto, I had changed the code around and neglected to remove that line. But it makes total sense that calling print within print recursively would cause a stack overflow.

Thanks for the speedy responses, guys. I appreciate the tips.

#7 SadKingBilly

  • Members
  • 160 posts

Posted 16 April 2013 - 01:03 PM

Oh God, I didn't even notice the other problems after I saw the goto. :lol:

Anyway, have you gotten it to work now?

#8 EarthwyrmJim

  • Members
  • 6 posts

Posted 16 April 2013 - 01:23 PM

It's close. No more stack overflows, but for some reason it claims my sum value is nil. I'll do some more testing. This is what I have thus far:
term.clear()
term.setCursorPos(1,1)
os.loadAPI("ocs/apis/sensor")
local functions = sensor.wrap("right")
local mfe
local total = 0
local count = 0
local mfeCount = 6
local maxStorage = 600000*mfeCount

local function recharge()
	turtle.down()
	sleep(5)
	turtle.up()
end

local function readMFE()
	while count <= mfeCount do
		turtle.down()
		count = count + 1
	end
	if turtle.getFuelLevel() < 30 then
		recharge()
	end
	while count > 0 do
		turtle.up()
		mfe = functions.getTargetDetails("0,0,1") -- the MFE column is south of the turtle.
		if mfe.Stored > 600000 then -- [b]Error on this line[/b]
			total = total + 600000
		else
			total = total + mfe.Stored
		end
		count = count - 1
	end
end

local function printTotal()
	count = 0
	--term.redirect(peripheral.wrap("front"))
	print(total)
	print(tostring(mfe.StoredPercentage).."%")
	total = 0
end

while true do
	readMFE()
	printTotal()
	sleep(.3)
end

EDIT: Line 29 is the error

#9 EarthwyrmJim

  • Members
  • 6 posts

Posted 16 April 2013 - 01:45 PM

I'm a dum dum. It works now. print(tostring(mfe.StoredPercentage).."%") was the issue, and my negligence will be the death of me.

#10 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 16 April 2013 - 02:12 PM

 EarthwyrmJim, on 16 April 2013 - 01:45 PM, said:

and my negligence will be the death of me.
Correction: death of us all

On topic: I'm glad to see you solved your own problem without the help of us. Kudos to you ^_^





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users