无法访问null对象引用的属性或方法

时间:2011-07-23 04:06:24

标签: flash actionscript

现在这让我发疯了..而且我花了一整天的时间来修复它.. :( 我只是不断收到这个错误(我不知道adobe怎么能给我们错误指向任何东西)

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at loader::AddPhoto()
    at AddPhoto()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at loader::AddPhoto()
    at AddPhoto()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at loader::AddPhoto()
    at AddPhoto()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at loader::AddPhoto()
    at AddPhoto()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()

这是我的代码我在舞台上有一个按钮(链接到as文件)和atextbox(没有链接或任何东西)

  
    

请igrnore杂乱的导入声明

  
package  loader 
{
    import flash.events.* ; 
    import flash.net.*;
    import flash.display.* ;
    import flash.media.* ;  
    import flash.display.Loader ; 
    import flash.display.LoaderInfo;
    import flash.system.*;
    import flash.text.TextField;
    import flash.utils.*;


    public class AddPhoto extends MovieClip {
        //variables
        private var FromURL:MovieClip;
        private var geurl:TextField;

        public var reques:URLRequest = new URLRequest (); //pass nothing yet
        public var getit:Loader = new Loader(); // the loader
        //constructor function 

        public function AddPhoto () {
            FromURL.addEventListener (MouseEvent.CLICK , addFromURL )  //  just a button


        }


        public function addFromURL (e:MouseEvent) {
            reques.url = geurl.text ;  // geurl is the txt box i have on the stage
            getit.load (reques);
            addChild(getit);

        }

    }

}

2 个答案:

答案 0 :(得分:1)

尝试:

private var FromURL:MovieClip = new MovieClip;

您声明了FromUrl变量,但是没有为它创建新实例。这可能是问题所在。

答案 1 :(得分:0)

让我猜一下。你正在使用Flash IDE(实际上并不是猜测。上面的堆栈跟踪正是你在IDE中得到的)并且你在舞台上有一个名为FormURL的子MovieClip。

不幸的是,Flash只会填充自定义类的公共属性,这意味着您需要公开该变量,或者您需要等到Event.ADDED并调用FormURL = MovieClip( getChildByName( "FormURL" ) ); < / p>

附注:AS3的首选样式是让所有变量都以小写字母开头。

的quickfix:

这应该有效。用这个替换你的构造函数:

public function AddPhoto(){ addEventListener( Event.ADDED, addedHandler ); }

private function addedHandler( event:Event ):void
{ 
     FromURL = MovieClip( getChildByName( "FormURL" ) ); 
}