Reputation: 173
I'm new to AS3 and I come from a Java background. In AS3, I have an initialized static const Object, PRESETS, and tried to access it in the constructor, but I get an error saying that the constant is null. Does a class constructor get called before the constants are initialized? I expect the constant to be ready to use before the constructor was called. Can anyone explain what is happening here? I would like to try and make this work.
My code below:
public class TteColor {
// This is the constant I'm trying to access from the constructor.
public static const PRESETS:Object = {
"WHITE": new TteColor("#FFFFFF"),
"BLACK": new TteColor("#000000"),
"GRAY": new TteColor("#808080"),
"RED": new TteColor("#FF0000"),
"GREEN": new TteColor("#00FF00"),
"BLUE": new TteColor("#0000FF"),
"YELLOW": new TteColor("#FFFF00"),
"CYAN": new TteColor("#00FFFF"),
"MAGENTA": new TteColor("#FF00FF")
};
public static const COLOR_REGEX:RegExp = /^#[\dA-Fa-f]{6}$/;
public var intValue:int;
public var strValue:String;
public function TteColor(color:String, defaultColor:TteColor = null) {
trace("trace0");
if (color != null && color.search(COLOR_REGEX) >= 0) {
trace("trace1");
strValue = color.toUpperCase();
intValue = uint("0x" + strValue.substring(1));
} else {
trace("trace2");
if (!defaultColor) {
trace("trace2.1");
trace("PRESETS: " + PRESETS);
defaultColor = PRESETS["WHITE"]; // PRESETS constant is still null here?
}
trace("trace3");
strValue = defaultColor.strValue;
intValue = defaultColor.intValue;
Logger.warning("Incorrect color value. Defaulting to: " + strValue);
}
}
}
Output:
The output shows that the PRESETS constant is null.
trace0
trace2
trace2.1
PRESETS: null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Changing to static variable
I change the PRESETS constant to static variable and statically initialize the values. This works fine as it should. Why would constant fails when static variable works?
// Statically initialize PRESETS
{
PRESETS = new Object();
PRESETS["WHITE"] = new TteColor("#FFFFFF");
PRESETS["BLACK"] = new TteColor("#000000"); PRESETS["GRAY"] = new TteColor("#808080");
PRESETS["RED"] = new TteColor("#FF0000");
PRESETS["GREEN"] = new TteColor("#00FF00");
PRESETS["BLUE"] = new TteColor("#0000FF");
PRESETS["YELLOW"] = new TteColor("#FFFF00");
PRESETS["CYAN"] = new TteColor("#00FFFF"); PRESETS["MAGENTA"] = new TteColor("#FF00FF");
}
// Changed from constant to static class variable. This works fine.
public static var PRESETS:Object;
Upvotes: 0
Views: 502
Reputation: 21
When using satic constant you are trying to create object of same class. So when doing a static const initialization it is failing.
Upvotes: 1
Reputation: 4649
Does a class constructor get called before the constants are initialized?
Normally no.
But you are explicitly calling your class constructor from within your static member:
"WHITE": new TteColor("#FFFFFF"),
"BLACK": new TteColor("#000000"),
// etc...
So when that static object is trying to create itself, it has to run the class constructor. Inside your class constructor you reference the static object, which hasn't finished creating itself.
Your second example works because you finish constructing the object before you start calling the class constructor. (The object is still empty at this point, but at least it's not null).
Upvotes: 4
Reputation: 359
The static members are members of the class itself, not on the instance. Therefore a call to static member will never call the constructor, because you're not creating an instance/object from the class, you're calling a function/member of a the class itself.
Upvotes: 3