简体   繁体   中英

Flex Error #1009: Cannot access a property or method of a null object reference

I am trying to use a button in my init() method.

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Test"
        creationComplete="init()">

Now when I try to do something with my button I get the error mentioned. I am assuming maybe it has not loaded yet?

function init():void{
     myButton.thisorthat == makes the error.
}

* EDIT ** The button is created in MXML btw Not that it matters, but this is for a flex mobile app.

Actually it does matter. One thing about NavigatorContent (assuming your children are a subset of one of these types of containers) to remember along with their halo counterparts is they all have a content creation policy set to deferred - meaning it creates the parent most layer of the view / viewstack, but not it's children until the user has actually navigated to that particular child. One cheat is to set the policy to 'ALL', but the better way is to actually listen for the FlexEvent.CONTENT_CREATION_COMPLETE instead (this is broadcast from the child of the navigation container).

Eg:

<halo:ViewStack id="setupStack" width="100%" height="100%">
    <api:FileSelector width="100%" height="100%" owner="{this}" 
                      enumerationMode="{FileSystemEnumerationMode.DIRECTORIES_ONLY}"
                      hint="{networkDbAccessHint}" />
    <!- this is valid, but not it's children until contentCreateComplete is fired -->
    <api:DataImport width="100%" height="100%" owner="{this}" />
</halo:ViewStack>

Both 'FileSelector' and 'DataImport' broadcast the event (extends s:NavigatorContent).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM