0
votes

I have this singleton that I'm using as a wrapper for global variables and constants, but as soon as I make some [Bindable] I get a crash on start up w/a bunch of red text in my console.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at BrandGlobals$/get COLOUR_EVERYTHING_BACKGROUND()[C:\MyProject\src\BrandGlobals.as:14]
    at BrandGlobals$cinit()
    at global$init()[C:\MyProject\src\BrandGlobals.as:2]
    at _mainWatcherSetupUtil/setup()
    at main/initialize()[C:\MyProject\src\main.mxml:0]
    at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2131]
    at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3400]
    at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3223]
    at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3069]

BrandGlobals:

package {
public final class BrandGlobals {
    [Bindable]public static var COLOUR_EVERYTHING_BACKGROUND:uint = 0xE010FF;

If I remove that [Bindable] and turn var to const there's no problem (except the obvious problem of not being able to set the variable outside of this file) but this doesn't work. Also, making the whole class [Bindable] instead of this one didn't work. When I hover my mouse over the COLOUR_EVERYTHING_BACKGROUND definition, it says "<exception thrown by getter>". 'Don't know what to think about that.

I might have guessed it was because it has no package, but I'm using another similar singleton which has [Bindable] variables and seems to work fine.

I never did get that [Bindable] twaddle.

I'm using the Flex 3.5 SDK.

I tried Brian's suggestion below, but it gave me pretty much the same error. I even tried:

{
    _COLOUR_EVERYTHING_BACKGROUND = 0xE010FF;
    trace("Var set."); //Breakpoint here
    bLoadedFerCryinOutLoud = true;
}

[Bindable]private static var _COLOUR_EVERYTHING_BACKGROUND:uint;
private static var bLoadedFerCryinOutLoud:Boolean = false;

public static function get COLOUR_EVERYTHING_BACKGROUND():uint {
    trace("Returning EVERYTHING background");
    if (bLoadedFerCryinOutLoud) 
        return _COLOUR_EVERYTHING_BACKGROUND;
    else return 0xFFFFFF;
}

What's more, if I put a breakpoint at that trace("Var set.");, Flash Builder complains that a break is not possible, because there is no executable code there.

I also noticed that in that call stack that I'm shown when this crash happens during a set and it seems to be the one that sets _COLOUR_EVERYTHING_BACKGROUND. But the only place where it is set is:

public static function SetBackground(oApp:UBIApplication):void {
    _COLOUR_EVERYTHING_BACKGROUND = oApp.nBackgroundColour;
}

and breakpoints indicate that this is never called.

3
Do you see the "Var set" trace when you run it?Brian
No, but I figured it out.Opux

3 Answers

0
votes

The documentation on using the tag has the following to say:

Using static properties as the source for data binding

You can use a static variable as the source for a data-binding expression. Flex performs the data binding once when the application starts, and again when the property changes.

You can automatically use a static constant as the source for a data-binding expression. Flex performs the data binding once when the application starts. Because the data binding occurs only once at application start up, you omit the [Bindable] metadata tag for the static constant. The following example uses a static constant as the source for a data-binding expression:

<fx:Script>
  <![CDATA[

    // This syntax casues a compiler error.
    // [Bindable]
    // public static var varString:String="A static var.";

    public static const constString:String="A static const.";
  ]]>
</fx:Script>

<!-- This binding occurs once at application startup. -->
<s:Button label="{constString}"/>    

0
votes

Edit: You need to make sure that your variable is initialized before you try to read it. A static initializer is the way to go:

package {
public final class BrandGlobals {
    {
        _COLOUR_EVERYTHING_BACKGROUND = 0xE010FF;
        trace("Var set."); //Breakpoint here
    }

    [Bindable]private static var _COLOUR_EVERYTHING_BACKGROUND:uint;

    public static function get COLOUR_EVERYTHING_BACKGROUND():uint {
        trace("Returning EVERYTHING background"); //Breakpoint here
        return _COLOUR_EVERYTHING_BACKGROUND;
    }

Putting in breakpoints in the places specified will let you verify that things are executing in the expected order

0
votes

It turns out that the problem was assigning COLOUR_EVERYTHING_BACKGROUND to a static const elsewhere in the code, as a temporary measure. Hopefully I'll remember that assigning [Bindable]s to static consts is bad and if I don't, I'll remember the meaning of that particular cryptic reaction Flash Builder had. I'm starting to choke StackOverflow w/my questions about cryptic error messages.