Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:38:43+00:00 2026-06-14T20:38:43+00:00

When building the code it builds fine but wont run. Debugging it causes a

  • 0

When building the code it builds fine but wont run. Debugging it causes a Nullreferenceexception error whenever a sprite uses variables from the sprite class (First instance at Setup()

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 BilliardsEngine2D
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Sprite CueBall;
        Sprite DirectionArrow;
        Sprite RedBall;

        SpriteFont myFont;

       //SoundEffect HitCue;
       //SoundEffect Collision;

        int ScreenWidth, ScreenHeight;

        float distance = 0.0f;
        public static float MasterVolume { get; set; }

        KeyboardState oldState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 500;
            graphics.PreferredBackBufferWidth = 750;
        }

        protected override void Initialize()
        {
            Setup();
            SoundEffect.MasterVolume = 0.01f;
            base.Initialize();
        }

        private void Setup()
        {
            ScreenHeight = graphics.GraphicsDevice.Viewport.Height;
            ScreenWidth = graphics.GraphicsDevice.Viewport.Width;
            CueBall.Rotation = 0.0f;
            DirectionArrow.Rotation = CueBall.Rotation;
            CueBall.Velocity = Vector2.Zero;
            CueBall.Position = new Vector2(ScreenWidth / 2, ScreenHeight / 2);
            DirectionArrow.Position = CueBall.Position;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            CueBall = new Sprite(Content.Load<Texture2D>("Sprites/CueBall"));
            DirectionArrow = new Sprite(Content.Load<Texture2D>("Sprites/Arrow"));
            RedBall = new Sprite(Content.Load<Texture2D>("Sprites/RedBall"));

            myFont = Content.Load<SpriteFont>("LucidaSans");
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();



            if (newState.IsKeyDown(Keys.Escape)) //Pause
            {
                Exit();
            }

            if (newState.IsKeyDown(Keys.Left)) //Rotate Left
            {
                CueBall.Rotation -= 0.05f;
                DirectionArrow.Rotation = CueBall.Rotation;
            }

            if (newState.IsKeyDown(Keys.Right)) //Rotate Right
            {
                CueBall.Rotation += 0.05f;
                DirectionArrow.Rotation = CueBall.Rotation;
            }

            if (newState.IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space)) //Power Up
        {
        }

        if (newState.IsKeyUp(Keys.Space) && oldState.IsKeyDown(Keys.Space)) //Fire
        {
            HitCueBall();
        }

        oldState = newState;

        SideCollision();
        //BallCollision();

        base.Update(gameTime);
    }

    private void AccelerateCueBall() //
    {
        CueBall.Velocity += new Vector2(
            (float)(Math.Cos(CueBall.Rotation - MathHelper.PiOver2) * 0.05f),
            (float)((Math.Sin(CueBall.Rotation - MathHelper.PiOver2) * 0.05f)));

        if (CueBall.Velocity.X > 7.5f)
        {
            CueBall.Velocity = new Vector2(5.0f, CueBall.Velocity.Y);
        }
        if (CueBall.Velocity.X < -7.5f)
        {
            CueBall.Velocity = new Vector2(-5.0f, CueBall.Velocity.Y);
        }
        if (CueBall.Velocity.Y > 7.5f)
        {
            CueBall.Velocity = new Vector2(CueBall.Velocity.X, 5.0f);
        }
        if (CueBall.Velocity.Y < -7.5f)
        {
            CueBall.Velocity = new Vector2(CueBall.Velocity.X, -5.0f);
        }
    }

    public void SideCollision()
    {
        CueBall.Position += CueBall.Velocity;

        if (CueBall.Position.X + CueBall.Width < 0)
        {
            CueBall.Position = new Vector2(ScreenWidth, CueBall.Position.Y);
        }
        if (CueBall.Position.X - CueBall.Width > ScreenWidth)
        {
            CueBall.Position = new Vector2(0, CueBall.Position.Y);
        }
        if (CueBall.Position.Y + CueBall.Height < 0)
        {
            CueBall.Position = new Vector2(CueBall.Position.X, ScreenHeight);
        }
        if (CueBall.Position.Y - CueBall.Height > ScreenHeight)
        {
            CueBall.Position = new Vector2(CueBall.Position.X, 0);
        }
    }

    private bool CheckBallCollision(Sprite CueBall, Sprite RedBall)
    {
        Vector2 position1 = CueBall.Position;
        Vector2 position2 = RedBall.Position;

        float Cathetus1 = Math.Abs(position1.X - position2.X);
        float Cathetus2 = Math.Abs(position1.Y - position2.Y);

        Cathetus1 *= Cathetus1;
        Cathetus2 *= Cathetus2;

        distance = (float)Math.Sqrt(Cathetus1 + Cathetus2);
        if ((int)distance < CueBall.Width)
            return true;

        return false;
    }

    private void HitCueBall()
    {


        Vector2 Velocity = new Vector2(
            (float)Math.Cos(CueBall.Rotation - (float)MathHelper.PiOver2),
            (float)Math.Sin(CueBall.Rotation - (float)MathHelper.PiOver2));

        Velocity.Normalize();
        Velocity *= 6.0f;
        CueBall.Velocity = Velocity;
        CueBall.Position = CueBall.Position + CueBall.Velocity;
    }

    protected override void UnloadContent()
    {

    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Green);

        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

        spriteBatch.Draw(CueBall.Texture, CueBall.Position, null, Color.White, CueBall.Rotation, CueBall.Center, CueBall.Scale, SpriteEffects.None, 1.0f);
        spriteBatch.Draw(DirectionArrow.Texture, DirectionArrow.Position, null, Color.White, DirectionArrow.Rotation, DirectionArrow.Center, 
            DirectionArrow.Scale, SpriteEffects.None, 1.0f);

        spriteBatch.End();
        base.Draw(gameTime);
    }
}

The Sprite Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace BilliardsEngine2D
{
class Sprite
{
    Texture2D texture;

    Vector2 position;
    Vector2 center;
    Vector2 velocity;

    float rotation;
    float scale;

    public Sprite(Texture2D texture)
    {
        this.texture = texture;

        position = Vector2.Zero;
        center = new Vector2(texture.Width / 2, texture.Height / 2);
        velocity = Vector2.Zero;

        Rotation = 0.0f;
        Scale = 1.0f;
    }

    public Texture2D Texture
    {
        get { return texture; }
    }

    public Vector2 Position
    {
        get { return position; }
        set { position = value; }
    }

    public Vector2 Center
    {
        get { return center; }
    }

    public Vector2 Velocity
    {
        get { return velocity; }
        set { velocity = value; }
    }

    public float Rotation
    {
        get { return rotation; }
        set
        {
            rotation = value;
            if (rotation < -MathHelper.TwoPi)
                rotation = MathHelper.TwoPi;
            if (rotation > MathHelper.TwoPi)
                rotation = -MathHelper.TwoPi;
        }
    }

    public float Scale
    {
        get { return scale; }
        set { scale = value; }
    }

    public int Width
    {
        get { return texture.Width; }
    }

    public int Height
    {
        get { return texture.Height; }
    }
}

Any ideas how to fix this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:38:45+00:00Added an answer on June 14, 2026 at 8:38 pm

    I believe that your setup method is getting called before the LoadContent method.

    According to this documentation LoadContent is called by Initialize.

        protected override void Initialize()
        {
            Setup();
            SoundEffect.MasterVolume = 0.01f;
            base.Initialize();
        }
    

    I’d say it’s fair to assume that base.Initialize() is where LoadContent is called from.

    I believe that your code is breaking, because you’re trying to set properties of your Sprite classes in the Setup() method before they’re instanciated in the LoadContent() method.

    I’d recommend moving statements where you modify your Sprite properties:

    CueBall.Rotation = 0.0f;
    

    after you’ve instantiated them, with new Sprite() in your LoadContent method.

    CueBall = new Sprite(Content.Load<Texture2D>("Sprites/CueBall"));
    

    in your LoadContent method

    Or you can move your Setup Call to your LoadContent method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into this error when building my code with CLANG: In file included
I have a portfolio that works fine but I'm currently building a CMS for
Building the project throws the following error from AAPT. I've rebuilt the project, but
I have a project that builds fine If I build it manually but it
For the optimum build time for Android Source code building (or any other Linux
I am building an XML document successfully with the following code: public function build($result)
When building code like this: <script type=text/javascript src=<%=ResolveUrl(~/js/js.js)%>></script> or <input type=image src=<%=ResolveUrl(~/img/submit.png)%> /> Should
More than one time I have encountered the following problem when building code with
I’m building MVC3 code first application and to display data on my page I’m
I've been using Visual Studio's property sheets for building my code and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.