5 Replies Latest reply on Jul 5, 2012 10:36 AM by Gaius Coffey

    Error when using URLLoader

    tjnelso Level 1

      I have been trying to read a text file from my local machine for a couple days now. Searching online I have found multiple tutorials that give examples similar to the following:

       

      http://sierakowski.eu/list-of-tips/47-loading-an-external-text-file-wi th-actionscript-3.html

       

      I have tried to duplicate this method but always get the same error message when trying to manipulate my URLLoader variable. My code is below:

       

      loaderError.PNG

       

      The error message reads: 

         

      Multiple markers at this line:

      -1120: Access of undefined property myLoader.

      -1120: Access of undefined property loadComplete.

       

      I can't seem to fix this error and have no idea what I'm doing wrong. This seems like such a simple task that I am getting very frustrated. Can anyone enlighten me?

        • 1. Re: Error when using URLLoader
          tjnelso Level 1

          I should note that in the code above I was in a Flex application trying to read a local file. I have also tried to read the file using an AIR application, as follows:

           

          importError.PNG

           

          In this example the errors read:

           

          First Error:

           

          Multiple markers at this line:

          -1120: Access of undefined property streamer.

          -1120: Access of undefined property dataFile.

           

           

          Warning:

           

           

          Multiple markers at this line:

          -fileContents

          -Access of undefined property bytesAvailable

          -Call to a possibly undefined method readUTFBytes

           

           

          Second Error:

           

          Multiple markers at this line:

          -Line breakpoint: LocalProject.mxml [line: 30]

          -1120: Access of undefined property fileContents.

          -fileContents

          -Call to a possibly undefined method close

          • 2. Re: Error when using URLLoader
            Gaius Coffey Level 2

            Hi,

            You need to remember that this is Flex and not Flash... the code you have written is part of the class definition and not timeline executed.

             

            What you need to do is this:

            1. Create a function including your loader code:

            <fx:Script>

            <![CDATA[

             

            import mx.collections.ArrayCollection;

            import mx.messaging.AbstractConsumer;

            private var data:ArrayCollection;

            private var myLoader:URLLoader;

            private function setMeUp():void {

                 var ur:URLRequest = new URLRequest("lineWipData.txt");

                 myLoader = new URLLoader();

                 myLoader.addEventListener(Event.COMPLETE,loadComplete);

                 myLoader.load(ur);
            }

            private function loadComplete(event:Event):void {

                 trace("Load completed with:\n"+myLoader.data);

            }

            ]]>

            </fx:Script>

             

            You then need to call the setMeUp() function somewhere, for example, you could put it in the application's creationComplete handler...

             

            As a side note, I doubt your "data as ArrayCollection" will work as the data will be received as String, so you need to parse the string into an ArrayCollection.

            G

            • 3. Re: Error when using URLLoader
              Gaius Coffey Level 2

              PS: Same goes for the AIR example.

              • 4. Re: Error when using URLLoader
                tjnelso Level 1

                Gaius,


                Thank you for the reply. I almost have it working now but not quite... I have the following code in my main application:

                 

                ~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN APPLICATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:mx="library://ns.adobe.com/flex/mx"
                      minWidth="955" minHeight="600"
                      xmlns:components="components.*"
                      creationComplete="application1_creationCompleteHandler(event)">

                 

                 

                 

                 

                  <fx:Script>
                   <![CDATA[
                   
                    import actionscript.FileLoad;
                    import mx.events.FlexEvent;
                   
                    protected function application1_creationCompleteHandler(event:FlexEvent):void
                    {
                     //Creates a FileLoad object
                     var dataLoad:FileLoad = new FileLoad();
                    }
                   
                   ]]>
                  </fx:Script>

                </Application>


                And the dataLoad object is an instance of the following class:


                ~~~~~~~~~~~~~~~~~~~~~~~~~ FileLoad Class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                package actionscript
                {
                     import flash.display.Sprite;
                     import flash.events.Event;
                     import flash.events.IOErrorEvent;
                     import flash.net.URLLoader;
                     import flash.net.URLRequest;
                     import flash.text.TextField;

                 

                     public class FileLoad extends Sprite
                     {
                            private var msg:TextField;
                            private var loader:URLLoader;
                            private var request:URLRequest;

                 

                 
                            //------------------------------------------------------------------------------
                            //Loads a text file saved in the bin-debug folder of the 'Project' Flex Project
                            //------------------------------------------------------------------------------
                            public function FileLoad()
                            {
                                  super();
                  
                                  request = new URLRequest("lineWipData.txt");
                                  loader = new URLLoader();
                                  msg = new TextField();
                  
                                  try
                                  {
                                        loader.load(request);
                                  }
                                  catch(error:SecurityError)
                                  {
                                        trace("A SecurityError has occured.");
                                  }
                  
                                  loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
                                  loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
                            }

                 

                            //------------------------------------------------------------------------------
                            //Once the constructor completes loading the text file, this method creates a
                            //text representation of the data and adds it to the main application panel
                            //------------------------------------------------------------------------------
                            private function loaderCompleteHandler(event:Event):void
                            {
                  
                                  try
                                  {
                                        msg.text = loader.data;
                                        msg.x = 10;
                                        msg.y = 10;
                                        addChild(msg);
                                  }
                                  catch (e:TypeError)
                                  {
                                        trace("Could not parse the file.");
                                  }
                            }
                 

                 

                            //------------------------------------------------------------------------------
                            //If the constructor throws an error loading the text file this method adds an
                            //error notification to the text field on the main application panel
                            //------------------------------------------------------------------------------
                            private function errorHandler(e:IOErrorEvent):void
                            {
                                  msg.text = "Had problem loading the .txt file.";
                            }
                 
                     }
                }


                But when I run the main application I just get a blank screen. I'm pretty sure the problem is in the Main Application code because I can take the 'FileLoad' class and use that code in its own, stand alone, ActionScript project and everything works fine. Do you know what I'm doing wrong since when I run the main application I'm not seeing the 'msg' textField with the data displayed on it? 

                 

                Thanks!

                • 5. Re: Error when using URLLoader
                  Gaius Coffey Level 2

                  [code]

                  <?xml version="1.0" encoding="utf-8"?>

                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                 xmlns:s="library://ns.adobe.com/flex/spark"

                                 xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

                   

                      <fx:Script>

                          <![CDATA[

                              private const DATA_FILE:String = "TxtFile.txt";

                   

                              protected function loadDataFile_clickHandler(event:MouseEvent):void

                              {

                                  currentState = "loadInProgress";

                                  var ur:URLLoader = new URLLoader();

                                  var r:URLRequest = new URLRequest(DATA_FILE);

                                  ur.addEventListener(Event.COMPLETE,processReceivedData);

                                  ur.addEventListener(SecurityErrorEvent.SECURITY_ERROR,handleError);

                                  ur.addEventListener(IOErrorEvent.IO_ERROR,handleError);

                                  try {

                                      ur.load(r);

                                  } catch(e:Error) {

                                      handleError(e);

                                  }

                              }

                              [Bindable] protected var loadedData:String;

                              protected function handleError(e:*):void {

                                  currentState = "data";

                                  loadedData = "Sorry, encountered an error loading:\n"+DATA_FILE+"\nThe error was:\n"+e;

                              }

                              protected function processReceivedData(event:Event):void {

                                  var ur:URLLoader = event.target as URLLoader;

                                  loadedData = ur.data;

                                  currentState = "data";

                              }

                          ]]>

                      </fx:Script>

                   

                      <fx:Declarations>

                          <!-- Place non-visual elements (e.g., services, value objects) here -->

                      </fx:Declarations>

                      <s:states>

                          <s:State name="nodata" />

                          <s:State name="loadInProgress" />

                          <s:State name="data" />

                      </s:states>

                   

                      <s:Button label="Click here to load data"

                                horizontalCenter="0" verticalCenter="0"

                                click="loadDataFile_clickHandler(event)"

                                includeIn="nodata"

                                />

                   

                      <s:Label horizontalCenter="0" verticalCenter="0"

                               text="Loading, please wait..."

                               includeIn="loadInProgress"

                               />

                   

                      <s:TextArea horizontalCenter="0" verticalCenter="0"

                                  text="{loadedData}"

                                   includeIn="data"

                                  />

                  </s:Application>

                  [/code]