constructor - How come I can't add an object to my list in c#? -


the following results in nullreferenceexception. tells me "drawobjs.add(drawobj);" - object reference not set instance of object. don't understand why not set instance?

the list:

list<drawobject> drawobjs; 

the method add's list:

public void createdrawobj()         {              drawobject drawobj = new drawobject(100,100,100,100,10);              drawobjs.add(drawobj);          }  

and class itself:

class drawobject     {          float _posx;         float _posy;         float _sizex;         float _sizey;         float _cr;          public drawobject(float posx, float posy, float sizex, float sizey, float cr)         {              _posx = posx;             _posy = posy;             _sizex = sizex;             _sizey = sizey;          }          public graphicspath objpath()          {              graphicspath path = new graphicspath();              path.addline(_posx + _cr, _posy, _posx + _sizex - (_cr * 2), _posy);             path.addarc(_posx + _sizex - (_cr * 2), _posy, _cr * 2, _cr * 2, 270, 90);             path.addline(_posx + _sizex, _posy + _cr, _posx + _sizex, _posy + _sizey - (_cr * 2));             path.addarc(_posx + _sizex - (_cr * 2), _posy + _sizey - (_cr * 2), _cr * 2, _cr * 2, 0, 90);             path.addline(_posx + _sizex - (_cr * 2), _posy + _sizey, _posx + _cr, _posy + _sizey);             path.addarc(_posx, _posy + _sizey - (_cr * 2), _cr * 2, _cr * 2, 90, 90);             path.addline(_posx, _posy + _sizey - (_cr * 2), _posx, _posy + _cr);             path.addarc(_posx, _posy, _cr * 2, _cr * 2, 180, 90);              path.closefigure();              return path;          }          public lineargradientbrush objbrush(int objcolor)         {              lineargradientbrush lgb;             if (objcolor == 1)             {                 lgb = new lineargradientbrush(new pointf(_posx + (_sizex / 2), _posy), new pointf(_posx + (_sizex / 2), _posy + _sizey), color.rosybrown, color.red);             }             else             {                 lgb = new lineargradientbrush(new pointf(_posx + (_sizex / 2), _posy), new pointf(_posx + (_sizex / 2), _posy + _sizey), color.greenyellow, color.green);             }             return lgb;          }      } 

you need instantiate drawobjs - without it, null reference:

list<drawobject> drawobjs = new list<drawobject>(); 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -