Recently I have upgraded my project with the latest Flex 4.1 SDK.  After a few small chokes, the project has largely been a smooth sail.  However, a mysterious error starts popping up everytime when my Flash movie starts:

TypeError: Error #1034: Type Coercion failed error at Flex movie start

The error – “TypeError: Error: #1034: Type Coercion failed” seems to throw even before the Flex application is initialized as none of stack trace information indicates code error from my Flex application.

I then created another empty Application in the same Flex project, strangely enough, I have the same error; but with a different project I see no issue.  This leads me to believe that something is  wrong with the library I have used.

Eventually, I have located the code:


/** Flag for initializing the styles */
private static var classConstructed:Boolean = classConstruct();

/** Initialize styles to default values */
private static function classConstruct():Boolean
{
if ( !FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration( "SomeComponent" ) )
{
// If SomeComponent has no CSS definition,
// create one and set the default value.
var newStyleDeclaration:CSSStyleDeclaration = new CSSStyleDeclaration();
newStyleDeclaration.setStyle( "selectorColor", 0xFF0000 );

FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration( "SomeComponent", newStyleDeclaration, true );
}
return true;
}

Apparently the code above causes the problem, which might have something to deal with deprecated code since Flex 3.

Through the exercise of hunting down this runtime error, I concluded the steps of troubleshooting such kind of error.

  1. Create an empty project with an empty movie, if it runs, that means Flex Compiler is healthy.
  2. Create an empty Application with the problematic application, if it runs, that means the Project has issue with its libraries; otherwise, the problem may lie in the code of the application.
  3. One-by-one, remove the SWC libraries referenced by the application, and run the empty Application everytime an SWC is removed until the error disppears.
  4. Troubleshoot the issue in the problematic SWC.

Happy Flexing!

Advertisements