c# - AccessViolationException while adding item to a list -
i developing windows phone 8.0 app in vs2010
and in point , decided make 2 classes (player,game)
game.cs
public class game { public string name { get; set; } public bool[] levelsunlocked { get; set; } public bool firsttimeplaying { get; set; } public game(int numoflevels) { this.firsttimeplaying = true; this.levelsunlocked = new bool[numoflevels]; } }
player.cs
public class player { public int id { get; set; } public string firstname{ get; set; } public string lastname { get; set; } public int age { get; set; } public int rank { get; set; } public int points { get; set; } public string rankdescreption { get; set; } public uri avatar { get; set; } public list<game> games; public player() { game hourglass = new game(6); game commonnumbers = new game(11); games.add(hourglass); games.add(commonnumbers); } }
when debug , app crashes @ line : games.add(hourglass);
because of accessviolationexception
, don't see problem of adding item list .
so ?
you must initialize list before using it.
this:
public list<game> games;
..needs this:
public list<game> games = new list<game>();
i surprised getting accessviolationexception
.. have expected nullreferenceexception
.
Comments
Post a Comment