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
                }
            }
        }

Recommended Answers

All 5 Replies

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)?

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.

commented: Good to point that out. Happened to me also! +14

@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.

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.

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
                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.