c# - Null reference when passing an array to a stored procedure -


i following error:

system.nullreferenceexception: object reference not set instance of object.

when try run unit test:

    [testmethod]     public void testdecreasetutorarea()     {         helpwith info = new helpwith();         info.subcategories[0] = 1;         info.userid = 14;          tutorservice tutorservice = new tutorservice();          tutorservice.decreasetutorarea(info);     } 

the helpwith class looks this:

public class helpwith {     public int userid { get; set; }     public int[] categories { get; set; }     public int[] subcategories { get; set; } } 

does know i'm doing wrong? seems me made clear info-subcategories is.

you haven't initialized arrays size. , trying access element

 info.subcategories[0] = 1; 

that why getting exception.

initialize them size before using or in constructor.

public void testdecreasetutorarea()     {         helpwith info = new helpwith();         info.subcategories = new int[10]; //here         info.subcategories[0] = 1;         info.userid = 14;          tutorservice tutorservice = new tutorservice();          tutorservice.decreasetutorarea(info);     } 

or in class constructor:

public class helpwith {     public int userid { get; set; }     public int[] categories { get; set; }     public int[] subcategories { get; set; }     //constructor     public helpwith()     {       this(10,10);     }      public helpwith(int categorysize, int subcategorysize)     {      categories = new int[categorysize]; //some size      subcategories = new int[subcategorysize];     } } 

if don't know size of array before hand use list<int>, remember initialize in constructor like:

public class helpwith {     public int userid { get; set; }     public list<int> categories { get; set; }     public list<int> subcategories { get; set; }     //constructor     public helpwith()     {         categories = new list<int>();         subcategories = new list<int>();     } } 

then using it:

[testmethod] public void testdecreasetutorarea() {     helpwith info = new helpwith();     info.subcategories.add(1);     info.userid = 14;      tutorservice tutorservice = new tutorservice();      tutorservice.decreasetutorarea(info); } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -