When using Flex you can't just put code in a script tag like this. While declarations (public var etc) can be in this scope, logic must be inside an event handler.
See my answer in this thread:
Thanks for your help. I will study the creationComplete event and try it.