8 Replies Latest reply on Jan 16, 2009 9:11 AM by rtalton

    Question About creationComplete

    Smile2JP
      Hi Guys, I'm starting leaning flex to build my first application. I'm having troubles about creationComplete event handler. I have two different functions need creationComplete to respond. But I'm encountering the error#1009, it says "Cannot access a property or method of a null object reference......" I tried applicationComplete method, it's not working.
      Could anyone show me how to deal with this problem? A lot of great flex component examples use creationComplete to complete the function, but what if I need to put all together? It might be really simple for you flex expert, I just couldn't figure out. :( Thanks for your help.
        • 1. Re: Question About creationComplete
          atta707 Level 2
          some minimum working code that shows the problem would be helpful for forum folks to help you.
          • 2. Re: Question About creationComplete
            Smile2JP Level 1
            atta707, Thanks for your reply.
            Here is what I got:
            ------------------------------------------------------------------------------------------ ---
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" x="0" y="0" xmlns:creativesource="it.creativesource.*" backgroundGradientAlphas="[0.86, 1.0]" backgroundGradientColors="[#2A2A2A, #040404]" alpha="1.0" creationComplete="init()" creationComplete="initCamera()">
            <mx:Script>
            <![CDATA[
            //upload image function
            private var fileRef:FileReference;
            public const FILE_UPLOAD_URL:String = " http://www.YOUR-WEBSITE-HERE.com/fileref/uploader.cfm";
            public function init():void {
            fileRef = new FileReference();
            fileRef.addEventListener(Event.SELECT, fileRef_select);
            fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
            fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
            }
            public function browseAndUpload():void {
            fileRef.browse();
            message.text = "";
            }
            public function fileRef_select(evt:Event):void {
            try {
            message.text = "size (bytes): " + numberFormatter.format(fileRef.size);
            fileRef.upload(new URLRequest(FILE_UPLOAD_URL));
            } catch (err:Error) {
            message.text = "ERROR: zero-byte file";
            }
            }
            public function fileRef_progress(evt:ProgressEvent):void {
            progressBar.visible = true;
            }
            public function fileRef_complete(evt:Event):void {
            message.text += " (complete)";
            progressBar.visible = false;
            }
            //webcam function
            import flash.media.Camera;
            import flash.media.Video;
            import mx.core.UIComponent;
            import mx.controls.Label;
            import flash.net.navigateToURL;
            import flash.net.sendToURL;
            import mx.graphics.codec.JPEGEncoder;

            public var videoHolder:UIComponent;
            public var cam
            public var video:Video;

            public function initCamera():void {
            var lblError:Label = new Label();
            try {
            videoHolder = new UIComponent();
            cam = Camera.getCamera();
            video = new Video(160,120);
            video.attachCamera(cam);
            videoHolder.addChild(video);
            cnvWebcam.addChild(videoHolder);
            cnvWebcam.addChild(lblError);
            }
            catch (err:TypeError){
            lblError.text = "Webcam error";
            cnvWebcam.addChild(lblError);
            }
            }

            public function takeSnapshot():void{
            var snapshotHolder:UIComponent = new UIComponent();
            var snapshot:BitmapData = new BitmapData(160,120, true);
            var snapshotbitmap:Bitmap = new Bitmap(snapshot);
            snapshotHolder.addChild(snapshotbitmap);
            cnvSnapshot.addChild(snapshotHolder);
            snapshot.draw(video);
            }

            public function saveImage():void{
            var bitmapData:BitmapData = new BitmapData(cnvSnapshot.width, cnvSnapshot.height);
            bitmapData.draw(cnvSnapshot);
            var encode:JPEGEncoder = new JPEGEncoder(100);
            var ba:ByteArray = encode.encode(bitmapData);
            var urlRequest:URLRequest = new URLRequest("jpg_reader.php");
            urlRequest.method = "POST";
            var urlVars:URLVariables = new URLVariables();
            urlVars.pic = ba;
            urlRequest.data = urlVars;
            flash.net.navigateToURL(urlRequest, "_blank");
            }
            ]]>
            </mx:Script>
            • 3. Re: Question About creationComplete
              rtalton Level 4
              You can only use the creationComplete tag once. Use commas to separate the two functions:
              creationComplete="init(), initCamera();"
              • 4. Re: Question About creationComplete
                Smile2JP Level 1
                Hi rtalton, Thank you for your answer. I learned something new. :) But It's still not working in my case. I got the same error. Does it matter if these two components (for each function) are in different state? I'm still thinking maybe this causes the problem?
                • 5. Re: Question About creationComplete
                  *gsb* Level 1
                  Yes. State matters.

                  • 6. Re: Question About creationComplete
                    rtalton Level 4
                    The error you are getting usually means you are trying to access an object (component, method) which does not exist yet.
                    Look at the line number for the error message and see what it is referring to. That's where you'll find the problem. Then change your code so you do not refer to that object until after it is created.
                    • 7. Re: Question About creationComplete
                      ntsiii Level 3
                      Do commas work? It is more typical to separate statements usning a semi-colon(;)
                      • 8. Re: Question About creationComplete
                        rtalton Level 4
                        yes, commas work