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 7808093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:03:01+00:00 2026-06-02T03:03:01+00:00

namespace SpaceInvader { /// <summary> /// This is the main type for your game

  • 0
namespace SpaceInvader
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SpaceInvaders : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D StarfieldImg;
        Texture2D InvaderImg;
        Texture2D AltInvaderImg;
        Texture2D RocketLauncherImg;
        Texture2D MissileImg;
        int RocketXPos;
        int AlienDirection;
        int AlienSpeed;
        Invader[] Invaders; 
        double Ticks;
        Missile MissileFired;

        public SpaceInvaders()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();

            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            RocketXPos = 512;

            AlienDirection = -1;
            AlienSpeed = 16;

            Invaders = new Invader[11];

            int XPos = 512;
            for (int Count = 0; Count < 11; Count++)
            {
                Invaders[Count] = new Invader();
                Invaders[Count].SetXPos(XPos);
                Invaders[Count].SetYPos(100);

                XPos = XPos + 32;
            }

            Ticks = 0;

            MissileFired = null;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            StarfieldImg = Content.Load<Texture2D>("Starfield1024x768");
            InvaderImg = Content.Load<Texture2D>("inv1");
            AltInvaderImg = Content.Load<Texture2D>("inv12");
            RocketLauncherImg = Content.Load<Texture2D>("LaserBase");
            MissileImg = Content.Load<Texture2D>("bullet");

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            MissileFired.Move();

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                MissileFired = new Missile(RocketXPos, 650);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                RocketXPos = RocketXPos - 4;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                RocketXPos = RocketXPos + 4;
            }

            if (RocketXPos < 100)
            {
                RocketXPos = 100;
            }

            if (RocketXPos > 924)
            {
                RocketXPos = 924;
            }

            Ticks = Ticks + gameTime.ElapsedGameTime.TotalMilliseconds;

            if (Ticks > 500)
            {
                for (int Count = 0; Count < 11; Count++)
                {
                    Invaders[Count].MoveHorizontal(AlienSpeed * AlienDirection);
                }

                if (Invaders[0].GetXPos() < 96)
                {
                    AlienDirection = +1;

                    int XPos = 96;
                    for (int Count = 0; Count < 11; Count++)
                    {
                        Invaders[Count].MoveVertical(4);
                        Invaders[Count].SetXPos(XPos);
                        XPos = XPos + InvaderImg.Width;
                    }
                }

                if (Invaders[10].GetXPos() > 924)
                {
                    AlienDirection = -1;

                    int XPos = 924 - InvaderImg.Width * 10;
                    for (int Count = 0; Count < 11; Count++)
                    {
                        Invaders[Count].MoveVertical(4);
                        Invaders[Count].SetXPos(XPos);
                        XPos = XPos + InvaderImg.Width;
                    }
                }

                Ticks = 0;
            }

            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(StarfieldImg, Vector2.Zero, Color.White);

            spriteBatch.Draw(RocketLauncherImg, new Vector2(RocketXPos, 650), Color.White);

            if (MissileFired != null)
            {
                Vector2 MissilePos = new
                Vector2(MissileFired.GetPosition().X, MissileFired.GetPosition().Y - MissileImg.Height);
            spriteBatch.Draw(MissileImg, MissilePos, Color.White);
            }



            for (int Count = 0; Count < 11; Count++)
            {
                spriteBatch.Draw(InvaderImg, Invaders[Count].GetPos(), Color.White);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

When attempting to run this game, MissileFired.Move is underlined with a null reference exception. I think it may have something missing in the draw or update method but i have no clue. The detailed information about this exception states the “Object reference not set to an instance of an object.” Help would be great, thank you!.

  • 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-02T03:03:02+00:00Added an answer on June 2, 2026 at 3:03 am

    The object MissileFired is null. When you attempt to call a member on a null object (such as Move()), you get a NullReferenceException.

    From what I can see in your code, MissileFired is conditionally created by a key press, so perhaps the code that attempts to use it should do a null check first and not assume it exists:

    if (MissileFired != null)
    {
        MissileFired.Move();
    }
    

    To me this makes some sense, a missile is only going to exist when it is fired, but the code needs to handle both situations.

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

Sidebar

Related Questions

using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
namespace MoqSample.Test { [TestFixture] public class GivenCustomerServiceTest { private ICustomerService customerService; private CustomerModel customer;
namespace WpfApplication1 { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial
namespace Dic { public class Key { string name; public Key(string n) { name
using namespace boost; class A {}; class B : public A {}; class X
Same namespace: 2 forms. public class Account //frm1 { public string Username; public string
Repository namespace MvcApplication1.Models { public class GroupRepository { EgovtDataContext db = new EgovtDataContext(); public
namespace std { class type_info { public: virtual ~type_info(); //type_info can serve as a
namespace pairDemo{ template<typename L, typename R> class pair{ public: pair(const L& left,const R& right)
API: namespace ClassLibrary1 { public class Class1 { public static string Test(string input) {

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.