2017-02-22 52 views
2

您好我有一個關於的FileStream openAsync讀取文件有問題,我有一個聽衆和fileReadCompleteHandler等待完整的FileStream openAsync引發Error#1009

var file:File = File.applicationStorageDirectory.resolvePath(fName+'.'+EXT); 
    var fileStream:FileStream = new FileStream(); 


    if (!file.exists) { 
       this.dispatchEvent(new AppEvent(AppEvent.DATA, null, false));           

    }else { 

     fileStream.addEventListener(Event.COMPLETE, fileReadCompleteHandler); 
     fileStream.openAsync(file, FileMode.READ); 
     fileStream.addEventListener(Event.CLOSE, fileClosedHandler); 
     fileStream.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler); 
    } 


    private function fileReadCompleteHandler(event:Event):void { 
     var ob:Object; 
     var fileStream:FileStream = FileStream(event.currentTarget); 

     try { 
      ob.source = fileStream.readObject(); 
     }catch (e:Error) { 
      trace('error:' + e.message) 
     } 

     fileStream.removeEventListener(Event.COMPLETE, fileReadCompleteHandler); 
     fileStream.close(); 

     } 

我得到錯誤:「錯誤#1009:無法訪問屬性或空對象引用的方法「。 我缺少什麼,如何從openAsync讀取對象?

感謝

+0

可以共享堆跟蹤錯誤?如果沒有行號,很難知道錯誤發生的位置。 – joshtynjala

回答

2

你永遠不會初始化目標ob,null對象的過程中訪問領域給你#1009:

var ob:Object; 
    var fileStream:FileStream = FileStream(event.currentTarget); 

    try { 
     ob.source = fileStream.readObject(); 

您需要:

var ob:Object = new Object; 
    var fileStream:FileStream = FileStream(event.currentTarget); 

    try { 
     ob.source = fileStream.readObject(); 
+0

你是對的Organis,這是我的錯誤,現在它按預期工作 –