0

似乎找不到这里有什么问题,我正在使用这种方法:

public void AddPlayerToTeam(Player player, Team team)
{
    Team t = new Team();
    if(team.PlayersList.Count>=20)
        return;
    if (!(team.PlayersList.Contains(player)))
        team.PlayersList.Add(player); 
}

稍后在此测试中:

[TestMethod]
public void CheckTeamOfPlayer9()
{
    Assert.AreEqual(wcm.GetPlayerById(9).PlayerTeam.CountryName, "Japan");
}

我收到以下错误:Object reference not set to an instance of an object.空引用异常。这是测试运行的方法:

public Player GetPlayerById(int playerId)
{
    var result = from b in Players
                 where b.PersonId.Equals(playerId)
                 select b;

    return result.FirstOrDefault(); 
}

有人有想法吗?

4

1 回答 1

1

return result.FirstOrDefault() will return a default value if thaere is no match. In the case of Player, that is null.

Check the result of wcm.GetPlayerById(9). Presumably it is null.

于 2013-11-12T18:34:13.910 回答