Null links

BlitzMax Forums/BlitzMax Beginners Area/Null links

Hi, confused why this code isnt working.

I'm defining the following:
link=list.firstLink()

while link and link.nextLink()
 currentObject=Object(link.value())
 nextObject=Object(link.nextlink().value())

wend


Now to my mind, the code shouldnt enter the WHILE loop unless link and link.nextLink() arent null (ie the objects they reference exist in the list). But my code enters the While loop, gets get to nextObject=Object(link.nextlink().value()) and complains about an unhandled exception.

I guess link.nextlink() is the problem, but I cant see the issue?

Thanks!

This doesn't enter the loop...
list:TList=CreateList()
link:TLink=list.firstLink()
While link And link.nextLink()
  Print "In here"
 currentObject=Object(link.value())
 nextObject=Object(link.nextlink().value())

Wend

Link and link.nextlink are both null as nothing has been added to the list. However, specifying link as TLink might have helped.
This is a simpler example...
list:TList=CreateList()
link:TLink=list.firstLink()
If link
  Print "link exists"
Else
  Print "link is null"
EndIf

<edit> Just in case...
list:TList=CreateList()
Type test
End Type
link:TLink=list.firstLink()
While link And link.nextLink()
  Print "In here"
 currentObject=Object(link.value())
 nextObject=Object(link.nextlink().value())
Wend
Print "outta here"
For x = 1 To 2
	my:test=New test
	ListAddLast list,my
Next
link:TLink=list.firstLink()
While link And link.nextLink()
  Print "In here"
 currentObject=Object(link.value())
 nextObject=Object(link.nextlink().value())
Wend
Print "outta here"


removed

here is the full code (I had actually added objects and made link type TLink - sorry should have posted this before)
	Method ShapeSearch()
		
		'Link for 'current' Path
		Local link:TLink
		
		'Link for adjacent paths to test against
		Local prevPathLink:TLink, nextPathLink:TLink
		
		'Balls for 'current' path
		Local currentBall:TBall, nextBall:TBall
		
		'Balls for adjacent paths to test against
		Local prevPathBall:TBall, nextPathBall:TBall
		
		'Link to first element in list
		link=ballList.FirstLink()
		prevPathLink=previousPath.ballList.firstLink()
		nextPathLink=nextPath.ballList.firstLink()
		
		Local count:Int=0
		'DebugStop	
			
		'As long as there are 2 balls in current path, and either the previous or next path have one
		While link And link.nextlink() And prevPathLink Or nextPathLink
			
			count:+1
	
				
			
			currentBall=TBall(link.value())
			nextBall=TBall(link.nextlink().value()) 'Unhandled exception here 
			
				If currentBall.colour=nextBall.colour
					
					If prevpathLink
						prevPathBall=TBall(prevPathLink.value())
								
						If currentBall.colour=prevPathBall.colour
								RemoveLink(prevPathLink)
								RemoveLink(link)
								RemoveLink(link.nextlink())
						EndIf
						
					EndIf																					
																											
					If nextPathLink
						nextPathBall=TBall(nextPathLink.value())
						If currentBall.colour=nextPathBall.colour
							RemoveLink(nextPathLink)
						EndIf	
					EndIf
					 
				
		
										
				link=ballList.firstLink()
				prevPathLink=previousPath.ballList.firstLink()
				nextPathLink=nextPath.ballList.firstLink()
				
				Else
					link=link.nextLink()
					prevPathLink=prevPathLink.nextLink()
					nextPathLink=nextPathLink.nextLink()
				EndIf
				
		Wend		
	EndMethod 	


I'm getting an unhandled exception error where I assign nextball=TBall(link.nextlink().value()) - not on the first iteration, usually the 4th time.

I dont understand how this could happen when the code should only enter the while loop if link AND link.nextLink() arent NULL?

Hmmm, that's not the full code either but never mind.
In addition, you haven't got a simple 'link and link.nextlink()' condition.
Anyway, is the following really doing what your comments say?
		While link And link.nextlink() And prevPathLink Or nextPathLink

What if you change it to...
while link and link.nextlink and (prevpathlink or nextpathlink)


Adding the parenthesis seems to solve that particular problem - what was it evaluating without them?

I believe it was evaluating...
while (link and link.nextlink and prevpathlink) or nextpathlink


Great. Thanks for the much needed assistance!