0

Hello! I have a little problem. I don't understand how to solve this issue:

Object reference not set to an instance of an object.

Thank you!

This is my code:

BindingList<ComboBox> cmbPlayers = new BindingList<ComboBox>();
Club[] league = new Club[10];

// league contains class Club
// class Club contains property Player[] (set as new Player[30])
// Players contains class Player
// Player has property String Name

        private void cmbTacticsAddPlayers() {

            foreach (var cmb in cmbPlayers)
            {
                for (int i = 0; i <= league[0].Players.Length;i++ )
                {
                    cmb.Items.Add(league[0].Players[i].Name); // THROWS EXCEPTION
                }
            }
        }
3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by niggz
0

Does it throw an exception on the FIRST loop?
If you take out that <= and just make it <, does that solve the problem (as it would be looping 1 too many times)?

1

league is defined as an array of Club[], but you never actually create any club objects and store them in league, so when you try to use it in like 15, there is nothing there to use.

Votes + Comments
Good to point that out. Happened to me also!
0

@thines01: No. The problem still exists. It seems like it goes trough the first time (adds player name in combo box 1), but than stops.

@Momerath: I'm sorry that I didn't provide this information, but league array is filled with Club objects using another method.

I've tried numerous ways, without success. But I realize something in this try:

private void cmbTacticsAddPlayers()
        {

            foreach (var cmb in cmbPlayers)
            {
                foreach (var player in league[0].Players)
                {
                    cmb.Items.Add(player.Name); // THROWS EXCEPTION
                }
            }
        }

I've gone trough Locals, and it says that variable 'player' is Null. How come? How to set it to 'new Player()'? I tried it outside, inside the loop, without success.

0

if

Club[] league = new Club[10];

,
then

foreach(Club c in league)
{
   //add mandatory players here
}

BUT THAT DEPENDS on how a Club object is created.
If a club has a player array or list that is automatically created, then you're OK.

My syspicion is that the player array in the club is not yet allocated.

0

I have solved my problem.

The players array contained players, but it was not full.

It looked something like this:

1. John Doe
2. Mark Markins
3. Bill Billy
4. null
5. null

so I just inserted one if:

private void cmbTacticsAddPlayers()
        {

            foreach (var cmb in cmbPlayers)
            {
                foreach (var player in league[0].Players)
                {
                    if (player == null)// solved the problem
                    {
                        continue;
                    }
                    else
                        cmb.Items.Add(player.Name); // THREW EXCEPTION
                }
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.