0

Ok, im trying to make something but it isnt working out for me as well as i thought it would. Bassicly im trying to Add to a list within an custom class array, is this makes any sense. Here is some code to perhaps make it easier to understand:

public class pfft
{
    public class Player
    {
        public string Name { get; set; }
        public int br { get; set; }
    }
    public class team
    {
        public int totalbr { get; set; }
        public List<Player> players { get; set; }
    }
}
public pfft.team[] Teams;
public int tmcount = 4;
private void btn_team_Click(object sender, EventArgs e)
{
    Teams = new pfft.team[tmcount]();
    foreach(pfft.Player p in getallplayers())
    {
        Teams[0].players.Add(p);
    }
}

This is bassicly all im doing, but when trying to add the player to the players list it crashes. Object reference not set to an instance of an object. The p it is trying to add does contain values, I checked that in the debugger I have also tried calling

Teams[0].players = new List<pfft.Player>();

but when i try that it will give the Nullexception at that part...

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Kage
  • 486
  • 1
  • 7
  • 18
  • You haven't initialized the array itself with any entries. You must assign a sized array to 'Teams' before addressing it with an index – Marvin Smit Nov 30 '13 at 21:52
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 30 '13 at 21:52

1 Answers1

1

You have two problems here:

  1. Creation of array do not create its objects:

    enter image description here

    so you need to iterate through array and create those objects:

    for (int i = 0; i < tmcount; ++i)
         Teams[i] = new pfft.team();
    
  2. You are not initializing public List<Player> players { get; set; } after Teams = new pfft.team[tmcount]();:

    Teams = new pfft.team[tmcount]();
    foreach(pfft.Player p in getallplayers())
    {
        Teams[0].players = new List<pfft.Player>();
        Teams[0].players.Add(p);
    }
    
  3. To not have to initialize players in each team, use a slightly different property:

    public class Team
    {
        private List<Player> players = new List<Player>();
    
        public int totalbr { get; set; }
        public List<Player> Players 
        {
            get { return players; }
            set { players = value; }
        }
    }
    
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • I did try so.. when i do that i get this: http://puu.sh/5y6tg.png Or better: http://puu.sh/5y6z2.png, And im not sure what you mean with the first problem. – Kage Nov 30 '13 at 22:02
  • Ahhhhh i see.. i was looking in the right direction.. just didn't realise i needed to make a 'new team' beside the list of players. Thank you very much! – Kage Nov 30 '13 at 22:10
  • 2
    The initialization of `players` needs to occur before the `foreach` loop, not inside it. – Sam Harwell Nov 30 '13 at 22:13
  • It should but OP is using only Teams[0] here. – Konrad Kokosa Nov 30 '13 at 22:14
  • @280Z28 That is indeed what i was initially doing.. but while trying to figure out what was going wrong i changed a lot to try stuff out.. – Kage Nov 30 '13 at 22:24