I always get a "An unhandled exception of type 'System.NullReferenceException' occurred in myapp.exe" when code reaches _match.Players2.Add(_hero);
I am not sure why, I initialized _match as well as _hero, when I debug I see that there are no null values. I am not sure why I am getting this exception, am I missing something?
Here is my code:
public static List<MyDotaClass.MatchHistory> GetMatchHistory(string uri)
{
var HeroCollection = new List<MyDotaClass.DotaHero>();
var MatchCollection = new List<MyDotaClass.MatchHistory>();
string response = GetDotaWebResponse(uri);
dotadata.Dota.MatchHistoryRootObject matches = JsonConvert.DeserializeObject<dotadata.Dota.MatchHistoryRootObject>(response);
foreach (var match in matches.result.matches)
{
var _match = new MyDotaClass.MatchHistory();
_match.MatchID = match.match_id.ToString();
foreach (var player in match.players)
{
var _hero = new MyDotaClass.DotaHero();
foreach(var h in heros)
{
if(player.hero_id.ToString().Equals(h.HeroID))
{
_hero.HeroName = h.HeroName;
_hero.HeroNonCleanName = h.HeroNonCleanName;
_hero.HeroID = h.HeroID;
_match.Players2.Add(_hero);
}
}
}
MatchCollection.Add(_match);
}
return MatchCollection;
}
public class MyDotaClass
{
public class DotaHero
{
public string HeroName { get; set; }
public string HeroNonCleanName { get; set; }
public string HeroID { get; set; }
}
public class MatchHistory
{
public string MatchID { get; set; }
//public List<DotaHero> Players { get; set; }
public List<MyDotaClass.DotaHero> Players2 { get; set; }
}
UPDATE:
For those interested in Dota, I lost all of my original code so I am rewriting and doing my best to make a tutorial out of it. http://uglyvpn.com/2014/07/21/dota-2-net-c-tool-pt1/