Well.. It's actually pretty simple.. I just created this real quick project to try out a new (and better ;)) way of loading images in XNA Game Studio 3.0 Visual Studio C# 2008 Express Edition but when I run & debug (F5) I get a warning telling me that I have a unhandled NullReferenceException in this line:

cannon_ball[i] = new GameObject(Content.Load<Texture2D>("Sprites\\ball"));

And this is my complete code (Game.cs):

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch sprite_batch;
        const int max_cannon_balls = 3;
        GameObject[] cannon_ball;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

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

            for(int i = 0; i < max_cannon_balls; i++)
            {
                cannon_ball[i] = new GameObject(Content.Load<Texture2D>("Sprites\\ball"));
                System.Console.WriteLine("Object loaded: " + cannon_ball[i]);
            }

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            sprite_batch.Begin();

            //Draw declarations here

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

Here's the code for the GameObject class:

using Microsoft.Xna.Framework.Graphics;

namespace Game
{
    public class GameObject
    {
        Texture2D loaded_texture;

        public GameObject(Texture2D loaded_texture)
        {
            this.loaded_texture = loaded_texture;
        }
    }
}

So help would really be appreciated!! :)

Recommended Answers

All 3 Replies

You need to size your cannon_ball array before using it:

cannon_ball = new GameObject[max_cannon_balls];
commented: Good +10

You need to size your cannon_ball array before using it:

cannon_ball = new GameObject[max_cannon_balls];

Wow dude thanks a lot! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.