Problem with data binding in Flex

This issue took me a while to figure out. It looks like errors inside function used for data binding aren’t reported to the Flash debug player. Take this code as an example.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	
	<mx:Script>
		<!&#91;CDATA&#91;
			
			private var _isValid:Boolean
			private var _myString:String;
			
			&#91;Bindable&#93;
			private function get isValid():Boolean
			{
				return _myString.length > 0;
			}
			
			private function set isValid( value:Boolean ):void
			{
				_isValid = value
			}	
		
		&#93;&#93;>
	</mx:Script>
	
	<mx:HBox width="100" height="100" 
		backgroundColor="{ isValid ? 0xFF0000 : 0x00FF00 }"/>
	
</mx:Application>

Based on the code you’d expect the background color to be either red or green. But if you run this the background color doesn’t get set at all. The reason is there’s an error inside ‘get isValid’, since _myString is null checking the value for length fails.

What makes this sort of problem really hard to track down is the compiler doesn’t generate an error when the call to determine the string’s length fails.

Hope this helps,
Hillel