I have two classes Player and Supporter. One of the fields of the class Player is a list of objects of class Supporter. when I try to add a new object to the supporter list for a certain player, I get NullReferenceException and I think it's because I never initialize the list of supporters, but I don't know where and how to initialize. Here is the code and the classes:
public class Player
{
public string name { get; set; }
public int numMention { get; set; }
public double s { get; set; }
public double salience { get; set; }
public double supportForPlayer1 { get; set; }
public double supportForPlayer2 { get; set; }
public double supportDiff { get; set; }
public double position { get; set; }
public List<Supporter> supporters { get; set; }
}
public class Supporter
{
public string name { get; set; }
public double GFP { get; set; }
public double supportAmount { get; set; }
}
foreach (var val in playersList)
{
sql = "SELECT Actor2CountryCode,SUM(GoldsteinScale) AS SupportForPlayer2 FROM [gdelt-bq:full.events] WHERE Year>=" + predictionDate + " AND (Actor1Code='" + val.name + "' AND (Actor2Code!='" + val.name + "' AND Actor2Code!=' ')) GROUP BY Actor2CountryCode";
response = new WebClient().DownloadString(url + "?q=" + Uri.EscapeUriString(sql));
value = JsonValue.Parse(response);
result = value as JsonObject;
for (int i = 0; i < result["modelData"]["rows"].Count; i++)
{
row = result["modelData"]["rows"][i];
if (row["f"][0]["v"] != null && row["f"][1]["v"] != null)
val.supporters.Add(new Supporter { name = (string)row["f"][0]["v"], supportAmount = (double)row["f"][1]["v"], GFP = 0 });
else
val.supporters.Add(new Supporter { name = "NULL", supportAmount = 0, GFP = 0 });
}
}