Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am receiving a \"A first chance exception of type \'System.NullReferenceExcept

ID: 639007 • Letter: I

Question

I am receiving a "A first chance exception of type 'System.NullReferenceException' occurred - Additional information: Object reference not set to an instance of an object." for:

effect.World = worldRot * worldTran * worldRot; (in the Draw)

Can someone please exlplain why?

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 Ference_First3DGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Camera camera;
        VertexPositionColor[] vertexColor;
        VertexBuffer vertexBuffer;
        BasicEffect effect;
        Matrix worldTran = Matrix.Identity;
        Matrix worldRot = Matrix.Identity;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        }
        //Initialize
        protected override void Initialize()
        {
          
            //Initialize camera
            camera = new Camera(this, new Vector3(0, 0, 10), Vector3.Zero,
                    Vector3.Up);
            Components.Add(camera);
        }

        //Load Content
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Vertices Color
            vertexColor = new VertexPositionColor[3];
            vertexColor[0] = new VertexPositionColor(new Vector3(0, 1, 0),
                Color.BlueViolet);
            vertexColor[1] = new VertexPositionColor(new Vector3(1,-1, 0),
                Color.Crimson);
            vertexColor[2] = new VertexPositionColor(new Vector3(-1, -1, 0),
                Color.LightSeaGreen);

            //Vertices Buffer
            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor),
                vertexColor.Length, BufferUsage.None);
            vertexBuffer.SetData(vertexColor);

            //Basic Effect
            effect = new BasicEffect(GraphicsDevice);
          
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        //Update
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // Translation WASD
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.A))
            {
                worldTran *=Matrix.CreateTranslation(-.01f, 0, 0);
            }
            if (keyboardState.IsKeyDown(Keys.D))
            {
                worldTran *= Matrix.CreateTranslation(.01f, 0, 0);
            }
            if (keyboardState.IsKeyDown(Keys.W))
            {
                worldTran *= Matrix.CreateTranslation(0, .01f, 0);
            }
            if (keyboardState.IsKeyDown(Keys.S))
            {
                worldTran *= Matrix.CreateTranslation(0, -.01f, 0);
            }

            base.Update(gameTime);

            // Rotation
            worldRot *= Matrix.CreateFromYawPitchRoll(MathHelper.PiOver4, 60, 0);
        }

        //Draw
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            GraphicsDevice.SetVertexBuffer(vertexBuffer);
          

            //Draw Triangle
            effect.World = worldRot * worldTran * worldRot;
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.VertexColorEnabled = true;

            //Effect and Draw
            foreach(EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawUserPrimitives<VertexPositionColor>
                    (PrimitiveType.TriangleStrip, vertexColor,0,2);
            }

            base.Draw(gameTime);
        }
    }
}

Explanation / Answer

Go to Debug -> Exceptions -> Search for NullReferenceException and check the "thrown" checkbox.

The point of "first-chance" exceptions is that you're seeing them pre-handler so that you can stop on them during debugging at the point of throwing. A "second-chance" exception is one that has no appropriate handler. Sometimes you want to catch "first-chance" exceptions because it's important to see what's happening when it's being thrown, even if someone is catching it.

There's nothing to be concerned with. This is normal behavior.