我正在尝试创建一个非常基本的Flash游戏,使用Tom Krcha的P2P游戏库,我发现here,我遇到的唯一问题是当我尝试实现该类时,我得到了我尝试启动应用时出错,
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Logger$/log()[C:\_DEV\P2PGameEngine\src\Logger.as:20]
at P2PGame/onConnect()[C:\_DEV\P2PGameEngine\src\P2PGame.as:54]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.adobe.fms::P2PSession/onNetGroupConnect()[C:\_DEV\P2PMessengerLib\src\com\adobe\fms\P2PSession.as:208]
at com.adobe.fms::P2PSession/netStatus()[C:\_DEV\P2PMessengerLib\src\com\adobe\fms\P2PSession.as:312]
这是我的Flash Builder代码,
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="520" height="391"
applicationComplete="init()">
<fx:Script>
<![CDATA[
private var game:P2PGame;
private const SERVER:String = "rtmfp://p2p.rtmfp.net/";
private const DEVKEY:String = "DEV_KEY_HERE";
protected function init():void
{
var usr:String = "user"+(Math.round(Math.random()*1000));
game = new P2PGame(SERVER+DEVKEY,"my_group");
game.addEventListener(Event.CONNECT, onGameConnect);
game.connect(usr);
}
private function onGameConnect(event:Event):void{
main_log.text += "P2P connection successfull..."
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextArea id="main_users" x="346" y="10" width="164" height="371" color="#FFFFFF"
contentBackgroundColor="#000000" text="ACTIVE USERS" textAlign="center"
textDecoration="underline" verticalAlign="top"/>
<s:TextArea id="main_log" x="10" y="10" width="328" height="371"/>
</s:WindowedApplication>
知道为什么会这样吗?我已经包含了这些库,但我仍然遇到了这个错误,任何想法?
提前致谢!
答案 0 :(得分:1)
错误消息在Logger class的log()函数中抛出。
除了正在访问其属性的TextField之外别无其他,所以这可能是罪魁祸首(txtArea
在某些时候是null
)。奇怪的是,源代码中有一个if语句,可以防止出现这种错误(if (txtArea != null)
)。你可以使用旧版本吗?您应该从github下载源代码,看看当前版本是否仍然会抛出相同的错误。
修改强>
我刚用SWC包创建了一个小测试:
package
{
import flash.display.Sprite;
public class Test extends Sprite
{
public function Test ()
{
Logger.log ("something");
}
}
}
跑吧,瞧:
TypeError:错误#1009:无法访问空对象引用的属性或方法。 在Logger $ / log() 在Test()
然而,使用来自github的源版本,一切运行正常(虽然没有做任何事情)。