Beyond Array length/bounds?

BlitzMax Forums/BlitzMax Beginners Area/Beyond Array length/bounds?

Amon(Posted 2007) [#1]
This keeps throwing an error for me.

		WriteMap:TStream = WriteFile(ThisFile)
		For Local yiter:Int = 0 Until MapWidth 
			For Local xiter:Int = 0 Until MapHeight 
				WriteString WriteMap,FactoryMap[xiter,yiter]
			Next
		Next
		CloseStream(WriteMap)


I can confirm that the file exists and is opened correctly as it writes the data in the array to the file.

It crashes with an "Unhandled Exception:Attempt to index array element beyond array length" error and BLide highlights Writestring.

Any ideas what could be causing it?

Thanks :)


JazzieB(Posted 2007) [#2]
It's not the writing to the file that's causing the problem, but reading from your FactoryMap array. The error would seem to suggest that the array hasn't be defined to the size of MapWidth and MapHeight, and is smaller than it should be. Start by checking where the array is defined/set-up.


BladeRunner(Posted 2007) [#3]
i assume you store the dimensions of factorymap[] as you init the array. As arrays count from 0, mapwidth and mapheight are 1 to big.

Second error that may be there is the fact that you iterate through mapheight with the x-axis and vice versa.


Amon(Posted 2007) [#4]
Hi JazzieB, thanks for the reply.

This is how i setup my MapWidth and MapHeight variables for the array.

Global MapWidth:Int = 1024/32
Global MapHeight:Int = (( 768 / 32 ) - 2)



Brendane(Posted 2007) [#5]
Double check your code - you are using y for the *width* and x for the *height*. Is this your intention? If your defined array is not square and you really meant x:=width, y:=height then that could be your problem.


Amon(Posted 2007) [#6]
Second error that may be there is the fact that you iterate through mapheight with the x-axis and vice versa.


OH NO! That was the problem. I can't beleive my project was on hold for 2 hours while I tried to figure out why that was happening.

It's fixed now, thanks to BladeRunner. :)

Thanks Dude. :) Thanks All :)


JazzieB(Posted 2007) [#7]
Didn't spot that he was getting MapWidth and MapHeight mixed up with the X and Y axis!

Anyway, as for your first sentence, when Until is used in a For loop BlitzMax stops before it reaches the last value, so would actually stop at MapWidth-1 and MapHeight-1, in this case. So this isn't the problem here. Probably just the whole X/Y mix-up thing.