The following code shoud throw an Error #1009: Cannot access a property or method of a null object reference:
var label:Label;
label.text = value;
However, it doesn’t if it’s inside of a setter which is set by MXML data binding:
public function set buggySetter(value:String):void {
var label:Label;
label.text = value; //will fail silently
}
To reproduce this weird behaviour, first, create a simple custom component by extending s:Label:
package {
import spark.components.Label;
public class BuggyLabel extends Label {
public function set buggySetter(value:String):void {
var label:Label;
label.text = value; //will fail silently
}
}
}
Sectond, add BuggyLabel to an Application and bind buggySetter:
<fx:Script>
<![CDATA[
[Bindable]
public var foo:String = 'NULL has no properties';
]]>
</fx:Script>
<local:BuggyLabel buggySetter="{foo}"/>
Why does this app fail silently?
The answer to that question is actually fairly short: it’s an architectural decision made by the Flex SDK engineers. If you take a look at the Flex source code, you’ll see a
try ... catchblock swallowing most errors thrown in a Binding.Pro: makes it easier to use bindings, since you don’t have to account for all possible faulty states
Con: it can be harder to debug (though if you know this can happen and you have good unit tests, you can reduce frustration from that side to nearly zero)
The source code I was talking about can be found in
mx.binding.Binding(in the ‘framework’ project) in methodwrapFunctionCall(). Here’s the relevant part: