A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: As3 cs5 # 1009 Null Reference

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3

    As3 cs5 # 1009 Null Reference

    I got a bunch of buttons in a simple calculator program, they function as they should in the document class without being imported. The main document also contains some text objects, however, these I'm unable to alter with AS3. I get the runtime error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Calculator()

    I tried having the text fields as movieclip objects and as regular text, they got their instances named defined and I checked the spelling. The function I call is: num1.restrict = "0-9"; The moment the code is //'d out the application works just fine, but when it's not it gets that runtime error.

    Can anyone see any obvious flaw? I didn't import flash.text.TextField; because I didn't need it with the buttons on the same page, I tried importing it but still had the same error. Any help is greatly appreciated. :3

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Well you didn't provide too much information. Like where is the textfield in relation to where you are attempting to modify it. Is num1 a textfield or the movieclip for that textfield?
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    The text field is placed in the middle of my Calculator.fla. The class I'm trying to access it from is Calculator.as, which is the main class and all other operations work in it. It's only a text field, nothing else. I tried having it as graphic and movieclip, all returned the same error.

  4. #4
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Still missing some information. I understand that the calculator class does all the work, but does it call the textfield, or does the text field exist prior? Is the textfield instance name 'num1'? The code to restrict the textfield is placed where, within the calculator class or on the main timeline? I can't open CS5 file here at work
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Image of stage: http://img.photobucket.com/albums/v7...tail/Stage.png
    Image of part of the class: http://img.photobucket.com/albums/v7...tail/Error.png

    It's all single layer single frame. I'm obviously a newbie since I post in this section, but I would assume that things added directly to the stage don't need to be called, since I'm able to operate with the buttons without calling them. =/ And thanks for the help by the way.

  6. #6
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    You doing pretty good. Let me tell you what I see. You have multiple textfields on the stage (main timeline) at runtime. Now at some point (which I can't verify) you call the calculator class, I'm going to assume that it is on the stage. Now the element you assign to the calculator class can be considered local to that class. Everything else is said to be outside of that class.

    This can be a little complicated at time. Because the num1 was not initially assigned to the calculator class it doesn't know it exist, which is where your problem is stemming from. So you have to find a way to point back to the main timeline and then to the num1 textfield.

    One way you can do this is to pass the object to the class through a method. Something like this:

    In your class you would have a method.
    Actionscript Code:
    function registerField(tf:TextField):void
    {
        stageFields.add(tf);
    }

    On the main timeline you would have the following
    Actionscript Code:
    var someObj:Calculator = new Calculator();
    someObj.registerField(num1);

    Now that passes the field to the calculator class. Then instead of calling num1 for make changes you would loop through the array or object and make the changes, like this
    Actionscript Code:
    stageField[0].restrict = "0-9";

    Now you may have to cast to identify the array element as a textfield, like this
    var tf:TextField = stageField[0] as TextField
    tf.restrict = "0-9";

    You'll have to play around with it.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center