c# - Nullreferenceexception after initializing an array -


this question has answer here:

i've been trying make test game in xna (visual studio 2015), nullreferenceexception every time load this, though initialize it. here short version of it...

    namespace overbox     {         public class player         {              private player[] characterlist = new player[14];               virtual public void initialize()              {                   //characterlist = new player[14];                   characterlist[0] = new hero();                    //for (int = 0; <= characterlist.length; i++)                   //    characterlist[i].initialize();                   characterlist[0].initialize();              }     }     virtual public void draw(spritebatch spritebatch, texture2d texture)     {         //for (int = 0; <= characterlist.length; i++)         //{         //    if (characterlist[i].active)         //        characterlist[i].draw(spritebatch, texture);         //}         characterlist[0].draw(spritebatch, texture); //here error occurs     }       } } 

if wants whole class reason edit this, related code find.

edit: stack trace

overbox.exe!overbox.player.draw(microsoft.xna.framework.graphics.spritebatch spritebatch, microsoft.xna.framework.graphics.texture2d texture) line 53 c# overbox.exe!overbox.mediahandler.draw(microsoft.xna.framework.graphics.spritebatch spritebatch) line 54 c#

overbox.exe!overbox.program.main(string[] args) line 15 c#

it's supposed there.

hero class:

    namespace overbox.characters     {          class jacketed : player          {          override public void initialize()          {                 //unrelated setting of variables          }          public override void draw(spritebatch spritebatch, texture2d texture)          {                 spritebatch.draw(texture, playerposition, color.white);          }     } } 

very short version of game loop class:

  public class game1 : microsoft.xna.framework.game   {         public static player player;         //i load content playertexture requires before asks         private texture2d playertexture;           protected override void initialize()         {                player = new player();                player.initialize();         }         protected override void draw(gametime gametime)         {                spritebatch.begin();                 player.draw(spritebatch, playertexture);                 spritebatch.end();         }   } 

private player[] characterlist = new player[14]; 

makes array of player, not construct objects. end array full of null values.

you missing this:

public player() {     foreach (var character in characterlist)     {         character = new player();     } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -