1

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 });
    }
}
David Mulder
  • 26,123
  • 9
  • 51
  • 114
nasim
  • 725
  • 2
  • 8
  • 17
  • And I will appreciate if somebody can comment why my code appears half code and half text here when I post a question? – nasim Dec 11 '14 at 18:24
  • That's because StackOverflow's markdown requires you to indent your code by 4 spaces to mark it as code... some of your code was indented by less than those 4 spaces. – Jcl Dec 11 '14 at 18:26
  • @nasim code should be indented at least 4 spaces. The lines that aren't indented 4 spaces show up as text. – D Stanley Dec 11 '14 at 18:26
  • I fixed the formatting issues. You have to make sure all the indentations are correctly in place. Please preview your question and verify its format before posting it. – Arian Motamedi Dec 11 '14 at 18:28
  • 1
    Wow, that last editing for code formatting came out really wrong :-) (the one by @DavidMulder, not the one by @PoweredByOrange) – Jcl Dec 11 '14 at 18:28
  • 1
    @Jcl Well, PoweredByOrange's looked all messed up in the review window, but that was one of the cases where the comparison window got it messed up, my bad, rolled back. Oh well, shouldn't have used my IDE's auto format for C#. – David Mulder Dec 11 '14 at 18:32
  • @DavidMulder , that's probably because there's code outside any classes :-) – Jcl Dec 11 '14 at 18:35

1 Answers1

1

You could add a constructor in Player, like:

public class Player {
   public Player() {
     supporters = new List<Supporter>();  
   }
}
Jcl
  • 27,696
  • 5
  • 61
  • 92