2
class Puzzle
      {
        private int PUZZLESIZE = 3;
        private int col, row;
        private Button[,] buttons;

        public Puzzle()
        { 
        }

        public Puzzle(Form1 form1)
        {
            buttons = new Button[3, 3] 
            { { form1.button1, form1.button2, form1.button3 }, 
            { form1.button4, form1.button5, form1.button6 }, 
            { form1.button7, form1.button8, form1.button9 } };          
        }

        public void reset()
        {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    buttons[i, j].BackColor = Color.Lime;
                }
        }

buttons[i, j].BackColor = Color.Lime;

this line causes NullReferenceException when I call the reset method in Form1.cs. Any advice is appreciated!!!

4
  • 2
    Although the button properties are available on the form, it is possible that they haven't been instantiated yet, and thus would be null. It depends on when you are trying to access them. Standard WinForms behaviour is to create all controls in InitializeComponent, though this isn't set in stone. Commented Oct 25, 2011 at 8:50
  • Are you sure your code path doesn't use the default constructor, which doesn't bind the buttons? Commented Oct 25, 2011 at 8:50
  • 1
    I would start by removing the empty constructor... Commented Oct 25, 2011 at 8:51
  • Where you call reset() from? How are the buttons in form1 built? Commented Oct 25, 2011 at 8:55

2 Answers 2

2

Two problems:

  • It's possible that form1.button1 (etc) are null. You should validate this in the constructor.
  • If you call the parameterless constructor instead of the Puzzle(Form1) constructor, buttons will still be null. I suggest you get rid of the parameterless constructor.

I'd also advise that you use braces for all loops, i.e.

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        buttons[i, j].BackColor = Color.Lime;
    }
}

Yes, it takes more space - but it's clearer and less error-prone in my experience.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you sure you're calling the default constructor? It looks like your class requires a Form1 instance to function, so try removing the default constructor. If it fails to build you've got your answer!

4 Comments

I think you are right. But how can I declare a puzzle instance with parameter in Form1.cs? Puzzle puzzle = new Puzzle(...);
You'd write Puzzle puzzle=new Puzzle(this);
It says "keyword 'this' is not available in the current context" stackoverflow.com/questions/7883686/… Please also check this one. Thank you!!!
That's probably because you're initializing it where you declare the member variable. Do the initialization in the Form1 constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.