0

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?

6
  • 1
    Please post more code. What is optionsList? I am guessing from the error this is a mobile project? Need more details.
    – Dom
    May 14, 2012 at 10:44
  • 1
    Assuming optionsList is a List component: List is not a container. You can't just add elements like that. You have to assign a custom ItemRenderer with a CheckBox.
    – RIAstar
    May 14, 2012 at 11:08
  • My bad, here is the MXML code: <s:Group id="optionsList"> <s:layout> <s:VerticalLayout /> </s:layout> </s:Group> May 14, 2012 at 11:25
  • @ChavdarSlavov You can format code in the StackOverflow editor by highlighting code and selecting the "Curly Bracket" button from the toolbar. You do not need to manually put line breaks in your questions. I tweaked your code. Most likely the reason for this error is that you are trying to add the child before the optionsList value is created; thus you get the "null access" Error. You could step through code using the Flex debugger to figure out which value is null. But, tell us when this code runs. All in all; I find that mixing MXML children w/ AS3 children is a recipe for disaster. May 14, 2012 at 13:12
  • Thanks @www.Flextras.com but i don't have installed toolbar extension. Is there any way to format my message without it ? I will make clean example and post it here, 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. May 14, 2012 at 14:09

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.