I'm a newbie C# programmer, and I would like to ask for help on this error.

I was trying to get a button to change its image when it is clicked, a task that sounds so simple, I know, but whenever I run the program and click said button, it returns a NullReferenceException error.

This is the code that causes the error, I think. The rest of the program is too long, even if it does nothing.

private void btnA1_Click(object sender, EventArgs e)
            if (a1 == false)

                btnPassChar = "A1";
                a1 = true;
                btnPassChar = "XX";
                a1 = false;
                MessageBox.Show("True to false");

I understand that putting in a path for the Image may not be the smartest thing to do. Still, it's the only thing on my mind right now.

If it's any help, I'm using Windows 7. Had the Visual Studio 2005 on Run as Admin rights. Also, the PNG file DOES exist. I made it myself.

Attachments NullReference.png 140.46 KB

You should use Image.FromFile():

btnA1.Image = Image.FromFile("G:/MCL/IT126L/CircuitCreator/CircuitCreator/bin/Debug/btnWhiteBack.png");


Edited 6 Years Ago by farooqaaa: n/a

Just so you know..the reason you were getting the error is because Image.Equals(object) is used to determine if the current image object is equal to the object specified in the paramaters. ie, PicBox1.Image.Equals(bitMap1) will return true if PicBox1.Image and bitMap1 are equal. In this sense 'equal' means they have the same bitwise representation.

This question has already been answered. Start a new discussion instead.