11 Replies Latest reply on Oct 17, 2009 4:25 AM by erthy

    Flex marking a sample code invalid "Access of undefined property"?

    erthy

      Hey,

       

      I used the code from this link: http://tdotblog.info/?q=node/6

       

      When i put it into Flex Builder 3, it has several issues with the code like "Access of undefined property FileStream" (or File).

      When I run the application, only the typical gray background appears.

       

      What am I doing wrong?

       

      Thanks, Erthy

        • 1. Re: Flex marking a sample code invalid "Access of undefined property"?
          Subeesh Arakkan Level 4

          Did you import it as an AIR app or Flex App ? File and FileStream classes are only available for AIR

          1 person found this helpful
          • 2. Re: Flex marking a sample code invalid "Access of undefined property"?
            erthy Level 1

            I did not import it, I just made a new project -> desktop application and pasted the code...

            When I tried to import it, I started a new project and i set the directory to the downloaded directory.

             

            Am I doing something wrong?

            • 3. Re: Flex marking a sample code invalid "Access of undefined property"?
              erthy Level 1

              This is the code:

               

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

              <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

              <mx:Script>

                   <![CDATA[

                        import mx.controls.Alert;

                        private function loadXML():void{

               

                             var file = new File();

                             file.addEventListener(Event.SELECT, dirSelected);

                             file.browseForOpen("Select person.xml file");

               

                        }

                        private function dirSelected(e:Event):void {

               

               

                             if(file.nativePath.indexOf("person.xml") != -1){

               

                                  var fs:FileStream = new FileStream();

                                  fs.open(file, FileMode.READ);

                                   var personXML = XML(fs.readUTFBytes(fs.bytesAvailable));

                                  fs.close();

                                  setTextInputs();

               

                             }else{

               

                                  Alert.show("You have not selected an xml file called 'person.xml'");

               

                             }

               

                        }

                        

                        private function setTextInputs():void{

               

                             fName_txti.text = personXML.firstName;

                             lName_txti.text = personXML.lastName;

                             save_btn.enabled = true;

               

                        }

                        

                        private function saveXML():void{

               

                             personXML.firstName = fName_txti.text;

                             personXML.lastName = lName_txti.text;

                             var newXMLStr:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + personXML.toXMLString();

                             var fs:FileStream = new FileStream();

                             fs.open(file, FileMode.WRITE);

                             fs.writeUTFBytes(newXMLStr);

                             fs.close();

               

                        }

                   ]]>

              </mx:Script>

               

               

              <mx:VBox>

               

              <mx:HBox>

               

              <mx:Label text="First Name:"/>

              <mx:TextInput id="fName_txti"/>

               

              </mx:HBox>

              <mx:HBox>

               

              <mx:Label text="Last Name:"/>

              <mx:TextInput id="lName_txti"/>

               

              </mx:HBox>

               

              <mx:HBox>

               

              <mx:Button id="load_btn" label="Load XML" click="loadXML()"/>

              <mx:Button id="save_btn" label="Save XML" click="saveXML()" enabled="false"/>

               

              </mx:HBox>

               

              </mx:VBox>     

              </mx:WindowedApplication>

              And it keeps saying: "Access of undefined property file" and "Access of undefined property personXML"

              What is wrong?

              • 4. Re: Flex marking a sample code invalid "Access of undefined property"?
                Subeesh Arakkan Level 4

                Looks like the downloaded example is not complete. If you are looking for a simple xml read/write in AIR, check this post

                http://www.adobe.com/devnet/air/flex/quickstart/xml_prefs.html

                1 person found this helpful
                • 5. Re: Flex marking a sample code invalid "Access of undefined property"?
                  erthy Level 1

                  When i import the project, Flex Builder says it is not a valid project.

                  When i run the PrefsXMLDemoFlex.xml through the adl.exe, nothing shows up.

                   

                  What is my problem?

                  • 6. Re: Flex marking a sample code invalid "Access of undefined property"?
                    Subeesh Arakkan Level 4

                    Create new application , copy and paste the code from the main application file in the example downloaded. It worked for me

                    • 7. Re: Flex marking a sample code invalid "Access of undefined property"?
                      erthy Level 1

                      Yes! The application is running!

                      Would you be ok, if I asked other questions in this topic as I try to put the code to use?

                      • 9. Re: Flex marking a sample code invalid "Access of undefined property"?
                        erthy Level 1

                        OK, so I used the code (btw thanks for the link, it is awesome, i already marked as much answers and helpful answers I could) and I came up with something like this:

                         

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

                        <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

                             <mx:Script>

                                  <![CDATA[

                                       import flash.filesystem.*;

                                       import mx.controls.Alert;

                                       

                                       public var prefsFile:File; // The preferences prefsFile

                                       [Bindable] public var prefsXML:XML; // The XML data

                                       public var stream:FileStream; // The FileStream object used to read and write prefsFile data.

                                       

                                       public function starter():void

                                  {

                                       // stage.nativeWindow.addEventListener(Event.CLOSING, windowClosingHandler);

                                       prefsFile = File.applicationStorageDirectory;

                                       prefsFile = prefsFile.resolvePath("slovnik.xml");

                                       readXML();

                                  }

                                  

                                       private function readXML():void

                                  {

                                       stream = new FileStream();

                                       if (prefsFile.exists) {

                                           stream.open(prefsFile, FileMode.READ);

                                           processXMLData();

                                       }

                                       else

                                       {

                                           Alert.show("Nenalezeny žádné slovníky!");

                                       }

                                       // stage.nativeWindow.visible = true;

                                  }

                                  

                                       private function processXMLData():void

                                  {

                                       prefsXML = XML(stream.readUTFBytes(stream.bytesAvailable));

                                       stream.close();

                                       // var slovicko = prefsXML.slovicko.cs;

                                       Alert.show("Česky je to: " + prefsXML.slovicko.@cs + "\n Anglicky je to: " + prefsXML.slovicko.@en);

                                       // stage.nativeWindow.x = prefsXML.windowState.@x;

                                       // stage.nativeWindow.y = prefsXML.windowState.@y;

                                       // stage.nativeWindow.width = prefsXML.windowState.@width;

                                       // stage.nativeWindow.height = prefsXML.windowState.@height;

                                  }

                                  

                                       private function writer():void

                                  {

                                       createXMLData();

                                       writeXMLData();

                                  }

                                  

                                       private function createXMLData():void

                                  {

                                       prefsXML = <slovicka/>;

                                       prefsXML.slovicko.@id = 2;

                                       prefsXML.slovicko.@cs = "kočka";

                                       prefsXML.slovicko.@en = "cat";

                                       // prefsXML.windowState.@width = stage.nativeWindow.width;

                                       // prefsXML.windowState.@height = stage.nativeWindow.height;

                                       // prefsXML.windowState.@x = stage.nativeWindow.x;

                                       // prefsXML.windowState.@y = stage.nativeWindow.y;

                                       // prefsXML.saveDate = new Date().toString();

                                  }

                                  

                                       private function writeXMLData():void

                                  {

                                       var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';

                                       outputString += prefsXML.toXMLString();

                                       outputString = outputString.replace(/\n/g, File.lineEnding);

                                       stream = new FileStream();

                                       stream.open(prefsFile, FileMode.WRITE);

                                       stream.writeUTFBytes(outputString);

                                       stream.close();

                                  }

                                  ]]>

                             </mx:Script>

                             <mx:Button id="start" label="Start" width="130"

                                          click="starter()"/>

                            <mx:Button id="write" label="Write" width="130"

                                          click="writer()" x="138"/>

                        </mx:WindowedApplication>

                        However, when I change the value, it overwrites the contents of the file. How can I just add one more "slovicko" element to the XML, so it would look like this:

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

                        <slovicka>

                          <slovicko id="1" cs="blah" en="blah" />

                          <slovicko id="2" cs="kočka" en="cat"/>

                        </slovicka>

                        after adding a new element, instead of just overwriting the previous content leaving only the id=2 element.

                         

                        Thanks, I failed to find it and I am sorry for my English.

                        • 11. Re: Flex marking a sample code invalid "Access of undefined property"?
                          erthy Level 1

                          Hey, I am trying to ask for a part of the XML file, and I want to change the variables I use to do that. In short words, why is my code not working?:

                           

                           

                          var slid = 3;

                          var lang = "en";

                          var slovicko = slovnikXML.slovicko.(@id == [slid]).@[lang];

                          Alert.show("Slovíčko je: " + slovicko);

                           

                           

                          even though this:

                           

                           

                          var slid = 3;

                          var lang = "en";

                          var slovicko = slovnikXML.slovicko.(@id == 3).@en;

                          Alert.show("Slovíčko je: " + slovicko);

                           

                           

                          is working perfectly --> not a problem in the slovnikXML variable.

                          this also works:

                           

                           

                          var slid = 3;

                          var lang = "en";

                          var slovicko = slovnikXML.slovicko.(@id == 3).@[lang];

                          Alert.show("Slovíčko je: " + slovicko);

                           

                           

                          So the probelm is somewhere around the @id part...

                          Any suggestions?

                          Thanks!