[英]NullReferenceException in XNA with C#
I am trying to make a simple 3D game for windows with XNA and C#. 我正在尝试使用XNA和C#为Windows创建一个简单的3D游戏。 I was trying to create view Matrices, as is suggested by every tutorial, and even MSDN, but I am getting a NullReferenceException error. 我试图按照每个教程甚至MSDN的建议创建视图矩阵,但是却出现了NullReferenceException错误。
The error reads: Object Reference not set to an instance of an Object. 该错误显示为:对象引用未设置为对象的实例。 It points to the definition of the projection matrix: 它指向投影矩阵的定义:
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
Here is my Code: 这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Series3D1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
Effect effect;
VertexPositionColor[] vertices;
Matrix viewMatrix;
Matrix projectionMatrix;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GraphicsDevice device;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Riemer's XNA Tutorials -- Series 1";
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
SetUpCamera();
effect = Content.Load<Effect>("effects");
SetUpVerticies();
device = graphics.GraphicsDevice;
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xProjection"].SetValue(projectionMatrix);
effect.Parameters["xWorld"].SetValue(Matrix.Identity);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
}
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1, VertexPositionColor.VertexDeclaration);
base.Draw(gameTime);
}
private void SetUpVerticies()
{
vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(0f, 0f, 0f);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(10f, 10f, 0f);
vertices[1].Color = Color.Green;
vertices[2].Position = new Vector3(10f, 0f, -5f);
vertices[2].Color = Color.Yellow;
}
private void SetUpCamera()
{
viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 50), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
}
}
}
You need to set your device = graphics.GraphicsDevice
before calling SetUpCamera()
. 您需要在调用SetUpCamera()
之前设置device = graphics.GraphicsDevice
。 The SetUpCamera
method requires that the device
field has already been assigned, which it hasn't in the current order you have. SetUpCamera
方法要求已分配device
字段,但尚未按照您当前的顺序进行分配。
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
SetUpCamera();
effect = Content.Load<Effect>("effects");
SetUpVerticies();
device = graphics.GraphicsDevice;
}
has to be 必须
protected override void LoadContent()
{
device = graphics.GraphicsDevice;
spriteBatch = new SpriteBatch(GraphicsDevice);
SetUpCamera();
effect = Content.Load<Effect>("effects");
SetUpVerticies();
}
Such that device
is set (which is required by SetUpCamera
and SetUpVerticies
). 设置该device
( SetUpCamera
和SetUpVerticies
)。
Debugging tip: Check out the locals to verify your assumptions, you would see device
is null
. 调试提示:检查本地人以验证您的假设,您会看到device
为null
。 You can also do this by hovering the variables in your line... 您也可以通过将行中的变量悬停来实现此目的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.