0

I'm trying to change the image of a pictureBox based on the number of occurance of a list:

list<string> items = new List<string>();
items.Add("Item1");
items.Add("Item2");
items.Add("Item3");
items.Add("Item4");
items.Add("Item5");

foreach (var item in items.OfType<string>().Select((x, i) => new { x, i }))
{
 int ItemNumber = item.i + 1;
 string ItemNumberStr = ItemNumber.ToString();
 PictureBox pbox = (PictureBox)this.Controls["Picturebox" + ItemNumberStr];
 pbox.Image = Properties.Resources.white_square_button;
 Label labl = (Label)this.Controls["label" + ItemNumberStr];
 labl.Text = item.x;
}

This is done within a foreach event in which item.i is the number of occurance represented as an int, then converted to a string to determine the number of pictureBox I'm trying to modify. But when I do this I recieve error "Object reference not set to an instance of an object" here:

pbox.Image = Properties.Resources.white_square_button;

This also happens with the label aswell.

What am I doing wrong?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Joscplan
  • 1,024
  • 2
  • 15
  • 35
  • 1
    What is null here? pbox? Properties.Resources? Something else? – Michael Petrotta Jul 06 '14 at 19:00
  • @MichaelPetrotta "pbox" – Joscplan Jul 06 '14 at 19:04
  • 1
    It looks like you don't have an element defined with `this.Controls["Picturebox" + ItemNumberStr]`. Make sure that you have such element before trying to access its value. – Darin Dimitrov Jul 06 '14 at 19:05
  • I believe the problem might be your naming convention. Are you sure the controls are named Picturebox1 ... Picturebox5, letter-case matching? Also, if the controls are nested, in another container, they will not be visible in the parent. If this is Windows Forms app, consider this.Controls.Find("PictureBox"+ItemNumberStr, true). – Darek Jul 06 '14 at 19:06
  • @Darek Did that and it indeed found the control i defined above. – Joscplan Jul 06 '14 at 19:16
  • is it "Picturebox"? or "PictureBox"? – The One Jul 06 '14 at 19:24
  • @216 "Picturebox". I changed the name. – Joscplan Jul 06 '14 at 19:25
  • Sweet, @JoseCardama. glad I could help. – Darek Jul 06 '14 at 19:25
  • 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 Jul 06 '14 at 20:26

2 Answers2

2

ControlCollection[String] will not throw an exception if there are no control with such name:

        Control control = this.Controls["I am not here"];
        MessageBox.Show((control == null).ToString());

It is doubtful that Properties.Resources property is null, so the most probable is that the control you are trying to access do not exist or named a bit different.

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
1

The PictureBoxes I was trying to modify were in a panel and as mentioned here: this.Controls doesn't contain all controls by doing this.Controls["Picturebox" + ItemNumberStr] it would return me a null reference because the PictureBoxes were assigned to that label. So what I did is replace this with panel1 (The name of the panel in which the PictureBoxes were) and now it works like a charm.

list<string> items = new List<string>();
items.Add("Item1");
items.Add("Item2");
items.Add("Item3");
items.Add("Item4");
items.Add("Item5");

foreach (var item in items.OfType<string>().Select((x, i) => new { x, i }))
{
 int ItemNumber = item.i + 1;
 string ItemNumberStr = ItemNumber.ToString();
 PictureBox pbox = (PictureBox)panel1.Controls["Picturebox" + ItemNumberStr];
 pbox.Image = Properties.Resources.white_square_button;
 Label labl = (Label)panel1.Controls["label" + ItemNumberStr];
 labl.Text = item.x;
}
Community
  • 1
  • 1
Joscplan
  • 1,024
  • 2
  • 15
  • 35