Note that there are some explanatory texts on larger screens.

plurals
  1. POAction Script 3.0 Vector not storing variables after Function exits
    text
    copied!<p>I am new to Action Script , i Have worked before with OOP languages but i am facing a problem , I have to build a brochure Application for a library , they handed me the data of their books in a XML format . I built two classes :Book and Library.</p> <p><strong>Book.as:</strong></p> <pre><code>package { import flash.display.Sprite; import flash.net.URLLoader; import flash.net.URLRequest; import flash.display.Loader; import flash.events.Event; import flash.display.Sprite public class Book extends Sprite { public var _author_name:String; public var _book_title:String; public var _book_subject:String; public var _book_edition:String; public var _ISBN: String; public var _publication_year:String; public var _price:String; public var _in_stock:String; public var _book_cover:String; public function Book() { } public function getAuthorName():String { return _author_name; } public function getBookTitle():String { return _book_title; } public function getBookSubject():String { return _book_subject; } public function getBookEdition():String { return _book_edition; } public function getISBN():String { return _ISBN; } public function getPublicationYear():String { return _publication_year; } public function getPrice():String { return _price; } public function getAvailability():String { return _in_stock; } public function getBookCover():String { return _book_cover; } public function setBookCover(temp:String):void { _book_cover = temp; } public function setAvailability(temp:String):void { _in_stock = temp; } public function setPrice(temp:String):void { _price = temp; } public function setPublicationYear(temp:String):void { _publication_year = temp; } public function setISBN(temp:String):void { _ISBN = temp; } public function setBookEdition(temp:String):void { _book_edition = temp; } public function setBookSubject(temp:String):void { _book_subject = temp; } public function setBookTitle(temp:String):void { _book_title = temp; } public function setAuthorName(temp:String):void { _author_name = temp; } public override function toString():String { var book_desc:String; book_desc = "Author Name: " + _author_name + "\n"; book_desc = book_desc + "Title: " + _book_title + "\n"; book_desc = book_desc + "Subject: " +_book_subject + "\n"; book_desc = book_desc + "Edition : " +_book_edition + "\n"; book_desc = book_desc + "ISBN: " +_ISBN + "\n"; book_desc = book_desc + "Publication Year: " + _publication_year + "\n"; book_desc = book_desc + "Price: " + _price + "\n"; book_desc = book_desc + "Available: " +_in_stock + "\n"; book_desc = book_desc + "Book Cover URL : " +_book_cover + "\n"; return book_desc; } public function equals(temp:Book):Boolean { if(_ISBN == temp.getISBN()) return true; else return false; } } } </code></pre> <p><strong>Library.as</strong></p> <pre><code>package { import flash.net.URLLoader; import flash.net.URLRequest; import flash.display.Loader; import flash.events.Event; import flash.display.Sprite; import flash.display.Sprite public class Library extends Sprite { public var xml:XML; public var url_loader:URLLoader; public var _book_vector:Vector.&lt;Book&gt; public var _xml_path:String; public function Library(xml_path:String) { _xml_path = xml_path; _book_vector = new Vector.&lt;Book&gt;(20); } public function getXMLPath():String { return _xml_path; } public function setXMLPath(temp:String):void { _xml_path = temp; } public function LoadXML():void { url_loader = new URLLoader(new URLRequest(_xml_path)); url_loader.addEventListener(Event.COMPLETE,ParseXML); } public function ParseXML(temp:Event):void { xml = new XML(temp.target.data); LoadBooks(); } public function LoadBooks():void { for(var i :int=0;i&lt;xml.children().length();i++ ) { _book_vector[i] = new Book(); _book_vector[i]._author_name = xml.children()[i].AuthorName; _book_vector[i]._book_title = xml.children()[i].BookTitle; _book_vector[i]._book_subject = xml.children()[i].Subject; _book_vector[i]._book_edition = xml.children()[i].Edition; _book_vector[i]._price = xml.children()[i].Price; _book_vector[i]._publication_year = xml.children()[i].Year; _book_vector[i]._in_stock = xml.children()[i].Availabilty; _book_vector[i]._book_cover = xml.children()[i].ImagePath; _book_vector[i]._ISBN = xml.children()[i].@ISBN; } } public function printLibrary():void { for(var i:int=0;i&lt;this._book_vector.length;i++) { trace(_book_vector[i].toString()); } } } } </code></pre> <p>The XML Loads fine , but when i try to call printLibrary() it throes a : Error #1009: Cannot access a property or method of a null object reference.</p> <p>when i use the debugger i can see that _book_vector is being loaded with "Books" and filed with data form the XML read , but when LoadBooks exits the vector holds them no more , why ? _book_vector is not local to the function! what have i Done wrong ? is there something i am not getting correctly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload