0
votes

In my main class, I have a list of "Entity" objects, Entity being a class I have created with all the necessary data I need for a player and/or enemies.

List<Entity> Listentities = new List<Entity>();

Farther down I have my Gravity method which, in its current state has some problems.

    public void Gravity(GameTime gameTime)
    {
        foreach(Entity entity in Listentities)
        {
            entity.entityPosition.X += 1;
        }
    }

It's very simple what I'm trying to do here; I am trying to take every Entity in the list and move its position.X down one unit every time the method is called. However, Every time I run the game, I get an "Object reference not set to an instance of an object" on the line

entity.entityPosition.X += 1;

I need to know what I'm doing wrong. While I am learning quickly, I am still rather new. Right now, only one entity is ever added to the list, the Entity "player1". I can easily fix this error by typing

player1.entityPosition.X += 1;

but that would only ever affect the player.

Edit: I am including the constructor for Entity:

    public Entity(string entityName, Texture2D EntityAppearance, int EntityMaxHealth, int SpriteHeight, int SpriteWidth, int CurrentFrame, int AnimationCall, int EntityAttack, int EntityStr, int EntityDex, int EntityLuk, int EntityDefense, bool EntityIsMe )
    {
        this.entityName = entityName;
        this.EntityAppearance = EntityAppearance;
        this.EntityMaxHealth = EntityMaxHealth;
        this.SpriteHeight = SpriteHeight;
        this.SpriteWidth = SpriteWidth;
        this.CurrentFrame = CurrentFrame;
        this.AnimationCall = AnimationCall;
        this.EntityAttack = EntityAttack;
        this.EntityLuk = EntityLuk;
        this.EntityDex = EntityDex;
        this.EntityStr = EntityStr;
        this.EntityDefense = EntityDefense;
        this.EntityIsMe = EntityIsMe;
    }

and the loadcontent() method in game1.

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        camera = new Camera(GraphicsDevice.Viewport);
        player1 = new Entity("player1", Content.Load<Texture2D>("PlaceHolder"), 100, 100, 100, 0, 0, 3, 1, 1, 1, 0, true);
        player1.EntityPosition = new Vector2(0, 0);
        player1.EntityBox = new Rectangle((int)player1.EntityPosition.X, (int)player1.EntityPosition.Y, player1.SpriteWidth, player1.SpriteHeight);
        player1.Origin = new Vector2();
        spritefont = Content.Load<SpriteFont>("SpriteFont1");
    }

and the entityPosition variables at the top of my Entity class.

    public Vector2 entityPosition;
    public Vector2 EntityPosition
    {
        get { return entityPosition; }
        set { entityPosition = value; }
    }

The initialization of the list:

    List<Entity> Listentities = new List<Entity>();

this is at the top of game1.cs

The code where I add the player1 Entity to Listentities:

    protected override void Initialize()
    {
        InitGraphicsMode(1280, 720, false);
        Listentities.Add(player1);
        base.Initialize();
    }

in Game1 of course.

Maybe I should have mentioned also that I am using XNA GS 4.0

5
Can you provide the code used to add entities, and possibly your entity class? My guess is something is not getting initialized correctly, either when creating the entity or when adding it to the list. Or entityPosition is a private field and can't be accessed in that manner.Opieus
Just something else: Instead of using new Vector2(0,0), you can do Vector2.Zero (which actually isn't required as vector2 is a struct and therefore has a default initialization which equals Vector2.Zero)Maxem
Please post the code where you add the entitiy(player) to the list. I'd assume that you add the Player before initializing it.Maxem
I don't see where you add player1 to Listentities.. Where is that code?Urjit
List initialization code added, along with other bits. Feel free to ask if you want to see anything else~Zanrkvil

5 Answers

1
votes

Perhaps you are not setting the value of the entity's entityPosition?

I would have expected entityPosition to be a Point structure and therefore not need initialization, but maybe you have your own EntityPosition class.

In that case,

someEntity.entityPosition = new EntityPosition();
0
votes

Ensure that each entity has created its entityPosition property before attempting to assign to X. Likely, you have forgotten to add a new EntityPosition() in one of your constructors somewhere.

0
votes

Can you post the part where you are creating the entities list? From the error message it is clear that you haven't initialized the list properly before using it.

For example look at this: http://blogs.msdn.com/b/csharpfaq/archive/2004/05/06/why-do-i-get-the-error-object-reference-not-set-to-an-instance-of-an-object.aspx

Also, to debug, just do one thing - print Listentities in the body of the method Gravity before the foreach loop.

0
votes

Make sure your entity and entityPosition is not null...

0
votes

Please ensure that the list is indeed initialized. Also make sure that you don't add null to the list.

Your EntityPosition is a Vector2 (Struct) and therefore cannot be null. (That is ofc assuming that you didn't create a class and named it vector2).