-1

I am trying to store an image to an Image list of a button control as:

Button btn = new Button();
btn.ImageList.Images.Add("image1", dbClass.Image1[0]); // ERROR
btn.ImageList.Images.Add("image2", dbClass.Image2[0]);

Where dbClass.Image1[0] is an image, but the code show this exception Object reference not set to an instance of an object.

What I am doing wrong here?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Farid-ur-Rahman
  • 1,809
  • 9
  • 31
  • 47

1 Answers1

2

Object reference not set to an instance of an object is a null reference exception and basically means that one of your values has not been instantiated yet and is set to null. I would hazard a guess that it is the ImagesList object which is null because you have created a new instance of a button which presumably doesn't instantiate that list by default, and therefore you cannot call the add method on it.

I would advise putting a breakpoint on the first line and debugging the code to check what the values are before the exception is thrown.

If it is the ImagesList that is null you need to instantiate it first like so:

btn.ImageList = new ImageList();
B-Lat
  • 1,685
  • 1
  • 18
  • 25