Null links

BlitzMax Forums/BlitzMax Beginners Area/Null links

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?