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="" layout="absolute">
			private var _isValid:Boolean
			private var _myString:String;
			private function get isValid():Boolean
				return _myString.length > 0;
			private function set isValid( value:Boolean ):void
				_isValid = value
	<mx:HBox width="100" height="100" 
		backgroundColor="{ isValid ? 0xFF0000 : 0x00FF00 }"/>

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,

One thought on “Problem with data binding in Flex”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s