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

java.lang.ArrayIndexOutOfBoundsException

Started by EarthwyrmJim, 16 April 2013 - 11:31 AM
EarthwyrmJim #1
Posted 16 April 2013 - 01: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.
SadKingBilly #2
Posted 16 April 2013 - 01: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.
oeed #3
Posted 16 April 2013 - 01: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.
oeed #4
Posted 16 April 2013 - 01:41 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.

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.
SuicidalSTDz #5
Posted 16 April 2013 - 01:49 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.

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
EarthwyrmJim #6
Posted 16 April 2013 - 01: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.
SadKingBilly #7
Posted 16 April 2013 - 02: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?
EarthwyrmJim #8
Posted 16 April 2013 - 02: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
EarthwyrmJim #9
Posted 16 April 2013 - 02: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.
SuicidalSTDz #10
Posted 16 April 2013 - 03:12 PM
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 ^_^/>