Tile editor

BlitzMax Forums/BlitzMax Beginners Area/Tile editor

Full source:
SuperStrict

Graphics 640,480,0,60

Const tsx:Int=32	'width of each tile
Const tsy:Int=32	'height of each tile
Const frms:Int=15	'# of frames in the tile set
Const timg$="samples\tileset1.png"	'the tile set image
Const tnumx:Int=20	'# of tiles across on your map
Const tnumy:Int=15	'# of tiles down on your map

Local mx:Int,my:Int	'mouse x/y
Local ctile:Int	'currently selected tile

Global map:tilemap=tilemap.Create(tsx,tsy,timg$,tnumx,tnumy,frms)

SetColor(255,255,255)

While Not KeyHit(KEY_ESCAPE)	

	mx=MouseX()/tsx*tsx
	my=MouseY()/tsy*tsy

	map.Draw(tnumx,tnumy,tsx,tsy,ctile)
	
	If KeyHit(KEY_RIGHT)
		ctile:+1
	EndIf
	
	If KeyHit(KEY_LEFT)
		ctile:-1
	EndIf
	
	If ctile>frms-1 Then ctile=0
	If ctile<0 Then ctile=14
	
	DrawImage(map.tset,mx,my,ctile)
	
	Flip;Cls
	
Wend
End

Type tilemap

	Field tset:TImage,tiles:tile[,]	
	
	Method Draw(tnumx:Int,tnumy:Int,tsx:Int,tsy:Int,ctile:Int)			
	
		For Local x:Int=0 To tnumx-1
			For Local y:Int=0 To tnumy-1
				tiles[x,y].Draw(x*tsx,y*tsy)
			Next
		Next
		
		If MouseDown(MOUSE_LEFT)
			tiles[MouseX()/tsx,MouseY()/tsy].tfrm=ctile
		EndIf		
	
	End Method
	
	Function Create:tilemap(tsx:Int,tsy:Int,timg$,tnumx:Int,tnumy:Int,frms:Int)
		
		Local tm:tilemap=New tilemap
			tm.tset=LoadAnimImage(timg$,tsx,tsy,0,frms)	
			tm.tiles=New tile[tnumx,tnumy]					
		
		For Local x:Int=0 To tnumx-1
			For Local y:Int=0 To tnumy-1								
				tm.tiles[x,y].tfrm=0									
			Next
		Next
		
		Return tm					
		
	End Function

End Type

Type tile
	
	Field tfrm:Int=0
	
	Method Draw(x:Int,y:Int)				
			
		SetColor(255,255,255)
		DrawImage(map.tset,x,y,tfrm)		
	
	End Method	
	
End Type

EDIT:
Let's try and cancel out the probable causes here. The full error says: Unhandled Exception:Attempt to access field or method of Null object on this line:
tm.tiles[x,y].tfrm=0


1) Tm doesnt exist. (Which it does)

You're right, tm does exist, we made a new one, then we returned it in the Create() function:
		Local tm:tilemap=New tilemap
			tm.tset=LoadAnimImage(timg$,tsx,tsy,0,frms)	
			tm.tiles=New tile[tnumx,tnumy]					
		
		For Local x:Int=0 To tnumx-1
			For Local y:Int=0 To tnumy-1								
				tm.tiles[x,y].tfrm=0									
			Next
		Next
		
		Return tm


2) X or Y are greater than tnumx or tnumy (Which you changed in both loop, right?)

Yes, as you can see both For loops are from 0 to tnumx/tnumy-1:
Draw() method:

		For Local x:Int=0 To tnumx-1
			For Local y:Int=0 To tnumy-1
				tiles[x,y].Draw(x*tsx,y*tsy)
			Next
		Next

Create() function:

		For Local x:Int=0 To tnumx-1
			For Local y:Int=0 To tnumy-1								
				tm.tiles[x,y].tfrm=0									
			Next
		Next

Also, tnumx and tnumy couldn't possibly change seeing as how they are both Const's set at the top of the code.

3 Tiles[,] is unallocated, which it isnt
or/and
4) tfrm is unallocated, which it isnt

Exactly, they both have predetermined values:
Field tset:TImage,tiles:tile[,]
and
Field tfrm:Int=0

So I just can't figure it out. Can you?