how to use insertafterlink?

BlitzMax Forums/BlitzMax Beginners Area/how to use insertafterlink?

flying willy(Posted 2005) [#1]
I have the following code:

If mh=1
	Local m:map=New map
	m.x=cursor.x
	m.y=cursor.y
	m.z=cursor.z
	maplist.InsertAfterLink m,cursor
EndIf


cursor and m are of type map, which has a single linked list called maplist.

cursor was made earlier and resides in the map. However this throws up an error: [ERROR]: Compile Error:Unable to convert from 'map' to 'TLink'


flying willy(Posted 2005) [#2]
I tried info I found elsewhere on these forums, noticably a post by dreamora, but the following snippet still doesn't compile:

'create one map entry for the cursor and make cursor handle pointer thing
'maplist.insertafterlink(m)
Local m:map=New map
Local t:tile = tile(tilelist.First())
t.selected=1
m.image=t.image
maplist.addlast(m)
cursor:map=m

Now there's a map entry called cursor which I can access. I'd like to be able to add something after the position cursor is at. I am sorting this list sometimes by Z position (it's a map editor with x,y,z hence the need for the cursor to also reside inside the map data)....

			'if click, add a new map item		
			If mh=1
				Local m:map=New map
				m.x=cursor.x
				m.y=cursor.y
				m.z=cursor.z
				maplist.InsertAfterLink m,maplist.findlink(cursor)
			EndIf


As you can see, I tried the insertafterlink stuff with cursor and a new map entry. It bombs badly.

The error when it crashes is: [ERROR]: Unhandled Exception: Attempt to access field or method of Null object


skidracer(Posted 2005) [#3]
Function InsertAfter(list:TList,a:Object,b:Object)
	Local	link:TLink
	link=list.FindLink(a)
	list.InsertAfterLink(b,link)
End Function

t:TList=New TList
t.AddLast "A"
t.AddLast "B"

InsertAfter t,"A","C"

For a$=EachIn t
	Print a
Next



flying willy(Posted 2005) [#4]
Thanks skid, a couple of questions if I may, as I am trying to learn blitzmax at the same time and want to understand why you are doing this.

Could someone explain how it works and why it works?


flying willy(Posted 2005) [#5]
okay it still bombs with the new function. I think I need to convert cursor somehow?

cursor was defined as:

Global cursor:map = m

Any clues as to why it does an unhandled exception?


skidracer(Posted 2005) [#6]
if you are adding an object after cursor in a list it will bomb if cursor has not alread been added to that list

maybe you added cursor to a different list?

the debugger is nearly there which should help you out a lot getting to grips with things but you hopefully can work out from the error message which object is null

here is a version with a little more error checking:

Function InsertAfter(list:TList,a:Object,b:Object)
	Local	link:TLink
	link=list.FindLink(a)
	Assert link Else "Can't find Object "+a.ToString()+" In List"
	list.InsertAfterLink(b,link)
End Function

t:TList=New TList
t.AddLast "A"
t.AddLast "B"

InsertAfter t,"D","C"

For a$=EachIn t
	Print a
Next



flying willy(Posted 2005) [#7]
Thank you for your help, I managed to see what I was doing wrong. Blitzmax might not be impossible after all!


bradford6(Posted 2005) [#8]
here are a few other LIST methods worth looking into (from an earlier thread)




Hotcakes(Posted 2005) [#9]
Blitzmax might not be impossible after all!

Which would be just as well, considering a whole language was built on it ;]