&引用;“未处理nullreferenceexception”;farseer物理引擎中的body.cs 我是一个C++程序员,试用C语言。我用C++做了很多BX2D,但这是我第一次和C语言合作。所以,我想用farseer物理引擎做一个简单的游戏。当我尝试编译代码时(我使用的是visual studio c#2010 express和xna game studio 4.0),它会在body.cs中的IBroadPhase broadPhase=World.ContactManager.broadPhase出现此错误:未处理nullreferenceexception。我相信问题出在我的玩家类中,下面是代码: public class Player : Entity { Vector2 position; SpriteBatch spriteBatch; Texture2D texture; Rectangle rectangle; Body body; CircleShape shape; Fixture fixture; public Player() { // TODO: Construct any child components here } /// /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// public override void Initialize(World world, SpriteBatch spriteBatch, Texture2D texture) { // TODO: Add your initialization code here this.spriteBatch = spriteBatch; this.texture = texture; rectangle = new Rectangle(0, 0, 11, 14); body = BodyFactory.CreateBody(world); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0, 0); shape = new CircleShape(1.0f, 1.0f); fixture = body.CreateFixture(shape); base.Initialize(world, spriteBatch, texture); } /// /// Allows the game component to update itself. /// /// <param name="gameTime" />Provides a snapshot of timing values. public override void Update(GameTime gameTime) { // TODO: Add your update code here base.Update(gameTime); } public override void Draw(GameTime gameTime) { spriteBatch.Begin(); spriteBatch.Draw(texture, new Vector2(body.WorldCenter.X * 10, body.WorldCenter.Y * 10), rectangle, Color.White); spriteBatch.End(); base.Draw(gameTime); } } 公共类玩家:实体 { 矢量2位置; SpriteBatch SpriteBatch; 纹理2D纹理; 矩形; 身体; 圆形; 夹具; 公共玩家() { //TODO:在此构造任何子组件 } /// ///允许游戏组件在启动前执行其需要的任何初始化 ///运行。在这里,它可以查询任何必需的服务并加载内容。 /// 公共覆盖无效初始化(世界、SpriteBatch SpriteBatch、Texture2D纹理) { //TODO:在此处添加初始化代码 this.spriteBatch=spriteBatch; 这个纹理=纹理; 矩形=新矩形(0,0,11,14); body=BodyFactory.CreateBody(世界); body.BodyType=BodyType.Dynamic; body.Position=新矢量2(0,0); 形状=新的圆形(1.0f,1.0f); fixture=body.CreateFixture(形状); 初始化(世界、spriteBatch、纹理); } /// ///允许游戏组件自我更新。 /// ///提供计时值的快照。 公共覆盖无效更新(游戏时间游戏时间) { //TODO:在此处添加更新代码 更新(游戏时间); } 公共覆盖无效绘制(游戏时间游戏时间) { spriteBatch.Begin(); 绘制(纹理,新矢量2(body.WorldCenter.X*10,body.WorldCenter.Y*10),矩形,颜色,白色); spriteBatch.End(); 基础。抽签(游戏时间); } }

&引用;“未处理nullreferenceexception”;farseer物理引擎中的body.cs 我是一个C++程序员,试用C语言。我用C++做了很多BX2D,但这是我第一次和C语言合作。所以,我想用farseer物理引擎做一个简单的游戏。当我尝试编译代码时(我使用的是visual studio c#2010 express和xna game studio 4.0),它会在body.cs中的IBroadPhase broadPhase=World.ContactManager.broadPhase出现此错误:未处理nullreferenceexception。我相信问题出在我的玩家类中,下面是代码: public class Player : Entity { Vector2 position; SpriteBatch spriteBatch; Texture2D texture; Rectangle rectangle; Body body; CircleShape shape; Fixture fixture; public Player() { // TODO: Construct any child components here } /// /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// public override void Initialize(World world, SpriteBatch spriteBatch, Texture2D texture) { // TODO: Add your initialization code here this.spriteBatch = spriteBatch; this.texture = texture; rectangle = new Rectangle(0, 0, 11, 14); body = BodyFactory.CreateBody(world); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0, 0); shape = new CircleShape(1.0f, 1.0f); fixture = body.CreateFixture(shape); base.Initialize(world, spriteBatch, texture); } /// /// Allows the game component to update itself. /// /// <param name="gameTime" />Provides a snapshot of timing values. public override void Update(GameTime gameTime) { // TODO: Add your update code here base.Update(gameTime); } public override void Draw(GameTime gameTime) { spriteBatch.Begin(); spriteBatch.Draw(texture, new Vector2(body.WorldCenter.X * 10, body.WorldCenter.Y * 10), rectangle, Color.White); spriteBatch.End(); base.Draw(gameTime); } } 公共类玩家:实体 { 矢量2位置; SpriteBatch SpriteBatch; 纹理2D纹理; 矩形; 身体; 圆形; 夹具; 公共玩家() { //TODO:在此构造任何子组件 } /// ///允许游戏组件在启动前执行其需要的任何初始化 ///运行。在这里,它可以查询任何必需的服务并加载内容。 /// 公共覆盖无效初始化(世界、SpriteBatch SpriteBatch、Texture2D纹理) { //TODO:在此处添加初始化代码 this.spriteBatch=spriteBatch; 这个纹理=纹理; 矩形=新矩形(0,0,11,14); body=BodyFactory.CreateBody(世界); body.BodyType=BodyType.Dynamic; body.Position=新矢量2(0,0); 形状=新的圆形(1.0f,1.0f); fixture=body.CreateFixture(形状); 初始化(世界、spriteBatch、纹理); } /// ///允许游戏组件自我更新。 /// ///提供计时值的快照。 公共覆盖无效更新(游戏时间游戏时间) { //TODO:在此处添加更新代码 更新(游戏时间); } 公共覆盖无效绘制(游戏时间游戏时间) { spriteBatch.Begin(); 绘制(纹理,新矢量2(body.WorldCenter.X*10,body.WorldCenter.Y*10),矩形,颜色,白色); spriteBatch.End(); 基础。抽签(游戏时间); } },c#,xna,xna-4.0,farseer,C#,Xna,Xna 4.0,Farseer,farseer测试床运行良好,因此我非常确定问题出在我的代码上,而不是farseer。如果您需要查看更多我的代码,请告诉我。我也在farseer论坛上发布了这个,所以如果我在那里得到答案,我会让你们知道的。提前谢谢。有人让我展示Game1.cs的代码,发现了问题。问题是,在初始化我的玩家之前,我从未构建过这个世界。通过添加world=newworld(Vector2.Zero)对其进行修正在我初始化播放器之前 我们可以得到完整的堆栈跟踪吗?请看:您还没有发布您所说的错误所在的代码行。无论如何,在

farseer测试床运行良好,因此我非常确定问题出在我的代码上,而不是farseer。如果您需要查看更多我的代码,请告诉我。我也在farseer论坛上发布了这个,所以如果我在那里得到答案,我会让你们知道的。提前谢谢。

有人让我展示Game1.cs的代码,发现了问题。问题是,在初始化我的玩家之前,我从未构建过这个世界。通过添加
world=newworld(Vector2.Zero)对其进行修正在我初始化播放器之前

我们可以得到完整的堆栈跟踪吗?请看:您还没有发布您所说的错误所在的代码行。无论如何,在该行上放置一个断点并调试代码。当它到达断点时,将鼠标悬停在对象上,查看哪个对象为空。当您尝试访问空对象的成员时,将出现此异常。找到空值并修复它。