私はActionScriptを初めて使用します。以前はオブジェクト指向言語を使用していましたが、問題が発生しています。図書館用のパンフレットアプリケーションを作成する必要があります。彼らは、書籍のデータをXML形式で渡してくれました。本と図書館の2つのクラスを作成しました。
Book.as:
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;
}
}
}
Library.as
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.<Book>
public var _xml_path:String;
public function Library(xml_path:String)
{
_xml_path = xml_path;
_book_vector = new Vector.<Book>(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<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<this._book_vector.length;i++)
{
trace(_book_vector[i].toString());
}
}
}
}
XMLは正常にロードされますが、printLibrary()を呼び出そうとすると、エラー#1009:nullオブジェクト参照のプロパティまたはメソッドにアクセスできません。
デバッガーを使用すると、_book_vectorに「Books」がロードされ、XML読み取りからのデータがファイルされていることがわかりますが、LoadBooksが終了すると、ベクターはそれらを保持しなくなります。なぜですか?_book_vectorは関数に対してローカルではありません!私は何を間違えましたか?正しく取得できないものはありますか?