c# - NullReferenceException was Unhandled - No idea why -


i'm trying code basic top down 2d game xna, , i've gotten part i'm trying add grass textures game , draw textures bottom portion of screen.

however, i'm getting "nullreferenceexception unhandled" error on line 32 of level.cs class. i'm new xna , new (in comparison) c#, can't figure out life of me.

update 1: mcmonkey4eva, error resolved. however, code stops during draw() method of testlevel class (level.cs). error still "nullreferenceexception unhandled" error, , methods bit different were.

anyone know i'm doing wrong here?


here's updated level.cs class:

    using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.content; using first.tiles; using first;   namespace first.level {     class testlevel     {         //an array of groundtiles set grass, lava, etc         public groundtile[,] groundlevel;          //width/height of level         int width, height;          grass grass;          public testlevel(int width, int height, contentmanager mycontent)         {             content = mycontent;             //i input screen dimensions level size             this.width = width;             this.height = height;              groundlevel = new groundtile[width, height];              grass = new grass(content.load<texture2d>(@"images\groundtiles"));         }          //drawing grass near bottom of screen         public void generateground()         {             (int y = 0; y < height; y++)             {                 (int x = 0; x < width; x += 32)                 {                     if (y == (height - 100))                     {                         if (groundlevel[x, y] == null)                         {                             groundlevel[x, y] = grass;                         }                      }                 }             }          }          public void draw(gametime gametime, spritebatch spritebatch)         {             foreach(groundtile ground in groundlevel)             {                 ground.draw(gametime, spritebatch); //here's error             }         }          public static contentmanager content         {             { return content; }         }         static contentmanager content;     } } 

here's updated game1.cs class:

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; using first.entity; using first.tiles; using first.level;  namespace first {     /// <summary>     /// main type game     /// </summary>     public class game1 : microsoft.xna.framework.game     {          const int screen_width = 600;         const int screen_height = 400;          graphicsdevicemanager graphics;         spritebatch spritebatch;          testlevel level;          usercontrolledsprite lancer;         texture2d lancertexture;         vector2 position = new vector2(200, 200);         point framesize = new point(32, 48);         int collisionoffset = 0;         point currentframe = new point(0, 0);         point sheetsize = new point(4, 4);         point spritetouse = new point(0, 0);         vector2 speed = new vector2(2, 2);         int millisecondsperframe = 500;          rectangle clientbounds;          public game1()         {             graphics = new graphicsdevicemanager(this);             content.rootdirectory = "content";         }          /// <summary>         /// allows game perform initialization needs before starting run.         /// can query required services , load non-graphic         /// related content.  calling base.initialize enumerate through components         /// , initialize them well.         /// </summary>         protected override void initialize()         {             // todo: add initialization logic here              clientbounds = graphics.graphicsdevice.viewport.bounds;             clientbounds.width = screen_width;             clientbounds.height = screen_height;              this.ismousevisible = true;              base.initialize();         }          /// <summary>         /// loadcontent called once per game , place load         /// of content.         /// </summary>         protected override void loadcontent()         {             // create new spritebatch, can used draw textures.             spritebatch = new spritebatch(graphicsdevice);              content.rootdirectory = "content";              level = new testlevel(screen_width, screen_height, content);             level.generateground();              lancertexture = content.load<texture2d>(@"images\lancer");             lancer = new usercontrolledsprite(lancertexture, position, framesize, collisionoffset,                                               currentframe, sheetsize, spritetouse, speed, millisecondsperframe);         }          /// <summary>         /// unloadcontent called once per game , place unload         /// content.         /// </summary>         protected override void unloadcontent()         {             // todo: unload non contentmanager content here         }          /// <summary>         /// allows game run logic such updating world,         /// checking collisions, gathering input, , playing audio.         /// </summary>         /// <param name="gametime">provides snapshot of timing values.</param>         protected override void update(gametime gametime)         {             // allows game exit             if (gamepad.getstate(playerindex.one).buttons.back == buttonstate.pressed)                 this.exit();              lancer.update(gametime, clientbounds);              base.update(gametime);          }          /// <summary>         /// called when game should draw itself.         /// </summary>         /// <param name="gametime">provides snapshot of timing values.</param>         protected override void draw(gametime gametime)         {             graphicsdevice.clear(color.cornflowerblue);              spritebatch.begin();             lancer.draw(gametime, spritebatch);             level.draw(gametime, spritebatch);             spritebatch.end();              base.draw(gametime);         }     } } 

here's grass.cs class:

using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.content; using first.tiles; using first;   namespace first.level {     class testlevel     {         //an array of groundtiles set grass, lava, etc         public groundtile[,] groundlevel;          //width/height of level         int width, height;          grass grass;          public testlevel(int width, int height, contentmanager mycontent)         {             content = mycontent;             //i input screen dimensions level size             this.width = width;             this.height = height;              groundlevel = new groundtile[width, height];              grass = new grass(content.load<texture2d>(@"images\groundtiles"));         }          //drawing grass near bottom of screen         public void generateground()         {             (int y = 0; y < height; y++)             {                 (int x = 0; x < width; x += 32)                 {                     if (y == (height - 100))                     {                         if (groundlevel[x, y] == null)                         {                             groundlevel[x, y] = grass;                         }                      }                 }             }          }          public void draw(gametime gametime, spritebatch spritebatch)         {             foreach(groundtile ground in groundlevel)             {                 ground.draw(gametime, spritebatch);             }         }          public static contentmanager content         {             { return content; }         }         static contentmanager content;     } } 

just in case, here's groundtile class too:

using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.content; using first.entity;  namespace first.tiles {     class groundtile     {          //public groundtile grass = new grass(content.load<texture2d>(@"images\groundtiles"));          // framesize needs modular, objects above walking-ground level          public texture2d texture;         protected point framesize;         public point frame;         public vector2 position;         int collisionoffset;         protected point sheetsize = new point(9, 19);          public groundtile(texture2d tiles, point frame, point framesize, int collisionoffset)         {             this.frame = frame;             this.framesize = framesize;             this.collisionoffset = collisionoffset;              this.texture = tiles;         }          //collision detection, incase of water or         public rectangle collisionrect         {                         {                 return new rectangle(                     (int)position.x + collisionoffset,                     (int)position.y + collisionoffset,                     framesize.x - (collisionoffset * 2),                     framesize.y - (collisionoffset * 2));             }         }          //not used, in case         public bool collideswith(groundtile tile, sprite sprite)         {             if (sprite.collisionrect.intersects(collisionrect))             {                 sprite.position -= sprite.direction;             }              return false;         }          public static contentmanager content         {             { return content; }         }         static contentmanager content;          public graphicsdevice graphicsdevice         {             { return graphicsdevice; }         }         graphicsdevice graphicsdevice;          public virtual void draw(gametime gametime, spritebatch spritebatch)         {             spritebatch.draw(texture,                 position,                 new rectangle(frame.x * framesize.x,                     frame.y * framesize.y,                     framesize.x, framesize.y),                     color.white, 0, vector2.zero,                     1f, spriteeffects.none, 0);         }      } } 

you're accessing content before it's set - or, rather, you're never setting @ all.

change

public testlevel(int width, int height) {     //i input screen dimensions level size 

to

public testlevel(int width, int height, contentmanager mycontent) {     content = mycontent;     //i input screen dimensions level size 

and add content argument when create testlevel object (i assume game1.cs) (note: sure create testlevel after create content object [in loadcontent method]!)

edit:

for new problem:

you're not defining contents of array, except single layer of grass...

the line

            ground.draw(gametime, spritebatch); 

change to

            if (ground != null)             {                  ground.draw(gametime, spritebatch);             } 

but should make sure ground getting filled content... specifically, new tile objects each , every point in array, not single line, , not same 'grass' instance every location.

i don't mean offend, you're dealing pretty basic errors here. might beneficial , follow basic c# tutorials.


Comments