我有1009个错误,但我找不到空对象。有人能给我指一下吗?
[故障]异常,信息=类型错误:错误#1009:无法访问null对象引用的属性或方法。
[故障]异常,信息=类型错误:错误#1009:无法访问属性或方法的空对象引用。在Square/draw(([/Users/si/Dropbox/ac3/Square/src/Square.as:21]Start/drawSquare(([/Users/si/Dropbox/ac3/square/src/Start.as:35]starling.events:EventDispatcher/invokeEvent(([/Users/redge/Dropbox/Development/sttaring/sttaring/src/sttaring/events/EventDispatcher.as:146]在starling.events:EventDispatchEvent(([/Users/redge/Dropbox/Development/sttaring/sttaring/src/sttaring/events/EventDispatcher.as:117]在starling.display::DisplayObject/dispatchEvent(([/Users/redge/Dropbox/Development/sttaring/sttaring/src/sttaring/deisplay/DisplayObject.as:398]在starling.display::DisplayObjectContainer/broadcastEvent(([/Users/redge/Dropbox/Development/staring/staring/src/staring/deisplay/DisplayObjectContainer.as:379]在starling.display::DisplayObjectContainer/broadcastEventWith(([/Users/redge/Dropbox/Development/staring/staring/src/staring/deisplay/DisplayObjectContainer.as:389]在starling.display::DisplayObjectContainer/addChildAt(([/Users/redge/Dropbox/Development/staring/staring/src/staring/deisplay/DisplayObjectContainer.as:135]在starling.core::starling/initializeRoot(([/Users/redge/Dropbox/Development/sttaring/sttaring/src/sttaring/core/starling.as:439]在starling.core::starling/initialize(([/Users/redge/Dropbox/Development/sttaring/sttaring/src/sttaring/core/starling.as:410]在starling.core::starling/onContextCreated(([/Users/redge/Dropbox/Development/sttaring/sttaring/src/sttaring/core/starling.as:649]
这是星光计划,它很重要。
这是代码:
package {
import starling.display.*;
import com.greensock.TweenLite;
import com.greensock.easing.Linear;
public class Square extends Sprite implements ISquare {
public const square:Quad = new Quad(100, 100);
private var direction:Boolean;
private var stopped:Boolean;
private var speed:int;
public function Square() {
}
public function draw():void{
addChild(square);
square.color = 0x4500FF;
direction = new Boolean(true);
stopped = new Boolean(false);
speed = new int(stage.stageHeight);
trace("vars init complete");
down();
}
...
以下是dropbox上的全部酸味:https://www.dropbox.com/sh/9x2q93o2ff1fsna/AADVJgt5nipDE1pdkgozkOc1a
好吧,让我们来看看相关的代码。。。
启动类
//stuff omitted like imports
public class Start extends Sprite {
public var viewport:Quad = new Quad(1,1);
private var swipe:SwipeGesture;
private var call:Square = new Square();
public function Start() {
super();
addEventListener(Event.ADDED_TO_STAGE, drawSquare);
}
private function drawSquare(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, drawSquare);
viewport.width = stage.stageWidth;
viewport.height = stage.stageHeight;
viewport.color = 0x414141;
addChild(viewport);
swipe = new SwipeGesture(viewport);
swipe.addEventListener(GestureEvent.GESTURE_RECOGNIZED, onSwipe);
trace("gesture listener added");
call.draw();
}
//stuff omitted
}
方形
//imports omitted
public class Square extends Sprite implements ISquare {
public const square:Quad = new Quad(100, 100);
private var direction:Boolean;
private var stopped:Boolean;
private var speed:int;
public function Square() {
}
public function draw():void{
addChild(square);
square.color = 0x4500FF;
direction = new Boolean(true);
stopped = new Boolean(false);
speed = new int(stage.stageHeight);
trace("vars init complete");
down();
}
//stuff omitted
}
当我手动读取您的代码时,我看到发生了以下情况:
- Start.constructor添加事件侦听器
- eventListener触发器,
start.drawSquare
- 添加视口,使用滑动功能
- 绘制正方形
- Square.draw称为
- 方形为自身添加四边形
- 设置一些颜色和变量
- 指向真
- 停止为false
- 至
stage.stageHeight
的速度
等等,stage
?CCD_ 4在CCD_。我认为它是无效的。事实上,当您查看错误的堆栈跟踪时,您可以看到这一点。它说at Square/draw() in /Users/si/Dropbox/ac3/square/src/Square.as:21
出了问题。Square.as的第21行是speed = new int(stage.stageHeight);
。这表明该行中提到的对象之一为空或未定义。通过消除过程,我们可以看到speed
已经被声明,并且正在被分配,因此是不相关的;int类已定义,否则编译会出错;并且我们只剩下CCD_ 9为空或者CCD_。
文档中说Stage.stageHeight
是一个int。它是stage的一个属性,由于它是一个基元类型,所以它从不为null或以其他方式未定义(无论如何,在actionscript中都不是(。因此,留给我们的是stage
:Start的call
实例从未添加到stage
中,因此stage为null,因此它在上为null
speed = new int(stage.stageHeight)
要解决此问题,请将Square的draw函数传递给stage
。。。或者,这似乎是更好的解决方案,考虑到您想要做的事情,在绘制之前将调用(即Square实例(添加到stage。