Chavdar Slavov
Chavdar Slavov

Reputation: 875

Flex mobile - Spark Chechbox Error

I'm using Flex SDK 4.6, very strange error occurs when i try to add spark checkbox on the stage:

var cb:CheckBox = new CheckBox();
optionsList.addElement(cb);

TypeError: Error #1009: Cannot access a property or method of a null object reference. at spark.skins.mobile::CheckBoxSkin/drawBackground()[E:\dev\4.y\frameworks\projects\mobiletheme\src\spark\skins\mobile\CheckBoxSkin.as:162] at spark.skins.mobile.supportClasses::MobileSkin/updateDisplayList()[E:\dev\4.y\frameworks\projects\mobiletheme\src\spark\skins\mobile\supportClasses\MobileSkin.as:313] at mx.core::UIComponent/validateDisplayList()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:8999] at mx.managers::LayoutManager/validateDisplayList()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:736] at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:819] at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]

When the chechbox is created in MXML there are no problems, but I need to create multiple instances in AS and than add them to stage.

Anyone have any clue what I'm doing wrong?

Upvotes: 0

Views: 240

Answers (1)

JeffryHouser
JeffryHouser

Reputation: 39408

I'm posting to clear up a misconception, which was in the comments, and I believe is the root of the error. To quote:

I checked when I add the checkbox on creationComplete it works, but in my case the checkboxes are added AFTER creationComplete when updateDisplayList is called.

updateDisplayList() is called, potentially, a lot during a component's lifecycle. It is called every time something on the screen needs to update itself. updateDisplayList() is called at least once before the creationComplete event is dispatched, however in some situations, with some components, it could be called more times.

The code you shared is taken a bit out of context, however as it is you will end up with a lot of checkboxes; because a new checkbox will be created after every iteration of updateDisplayList(). I strongly recommend you move your children creation to the createChildren() method instead of updateDisplayList().

Here is some documentation on the Spark Flex Component LifeCycle and some on MX Component LifeCycle.

Upvotes: 1

Related Questions