954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Object reference not set to an instance of an object - How to?

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
                }
            }
        }
niggz
Light Poster
26 posts since Jun 2011
Reputation Points: 10
Solved Threads: 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)?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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.

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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

niggz
Light Poster
26 posts since Jun 2011
Reputation Points: 10
Solved Threads: 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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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
                }
            }
        }
niggz
Light Poster
26 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: