AccessViolationException при добавлении элемента в список

1

Я разрабатываю приложение Windows Phone 8.0 в VS2010

и в какой-то момент я решил сделать 2 класса (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);

        }
}

Когда я отлаживаю, приложение вылетает в Line: Games.Add(HourGlass); из-за AccessViolationException, я не вижу, в чем проблема добавления элемента в список.

так что это?

Теги:
windows-phone-8

2 ответа

2
Лучший ответ

Вы должны инициализировать список перед его использованием.

Эта:

public List<Game> Games;

.. Нужно быть следующим:

public List<Game> Games = new List<Game>();

Я удивлен, что вы получаете AccessViolationException.. Я бы ожидал NullReferenceException.

1

Вы не установили свои игры в новый список.

 public List<Game> Games = new List<Game>();

        public Player()
        {
            Game HourGlass = new Game(6);
            Game CommonNumbers = new Game(11);

            Games.Add(HourGlass);
            Games.Add(CommonNumbers);

        }

Ещё вопросы

Сообщество Overcoder
Наверх
Меню