0

I have 30 or so textboxes that I have labeled in order {ex: R1C1B1, R1C2B1, R2C1B1, ...}

Here is my code:

    private void Solve(int row, int column, int box)
    {
        string numbers = "123456789";
        TextBox newTextBox = (TextBox)Controls.Find(string.Format("tbox{0}", "R" + row.ToString() + "C" + column.ToString() + "B" + box.ToString()), false).FirstOrDefault();

        MessageBox.Show(newTextBox.Text);

        if (newTextBox.Text != "")
         {
            return;
         }

        numbers = numbers.Replace(checkRow(row, column), "");
        numbers = numbers.Replace(checkColumn(row, column), "");
        numbers = numbers.Replace(checkBoxPos(row, column), "");

        if (numbers.Length == 1)
         {
            newTextBox.Text = numbers;
         }
    }

Whenever I try to use MessageBox.Show to view what is in the textbox I get the error in the title. Here is all of the error:

An unhandled exception of type 'System.NullReferenceException' occurred in SudokuSolver.exe

Additional information: Object reference not set to an instance of an object.

Sergey Berezovskiy
  • 215,927
  • 33
  • 392
  • 421
Domoq
  • 15
  • 1
  • 6

3 Answers3

1

You should check whether newTextBox is not null before accessing it. If there is no textbox with given name, then FirstOrDefault will return null as default value:

var name = String.Format("tboxR{0}C{1}B{2}", row, column, box);
TextBox newTextBox = (TextBox)Controls.Find(name, false).FirstOrDefault();

if (newTextBox != null)
{
    MessageBox.Show(newTextBox.Text);
    //...
}

Remember, if newTextBox is null then you cannot get it's text - null.Text will give you NullReferenceException.

BTW you can use LINQ to search controls

var textBox = Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == name);
Sergey Berezovskiy
  • 215,927
  • 33
  • 392
  • 421
1

Instead of newTextBox.Text != "" use newTextBox != null && newTextBox.Text != ""

Selman Genç
  • 94,267
  • 13
  • 106
  • 172
0

Try like this:

private void Solve(int row, int column, int box)
    {
        string numbers = "123456789";
        TextBox newTextBox = (TextBox)Controls.Find(string.Format("tbox{0}", "R" + row.ToString() + "C" + column.ToString() + "B" + box.ToString()), false).FirstOrDefault();

         if(newTextBox!=null)
         {
             MessageBox.Show(newTextBox.Text);
             return;
         }

        numbers = numbers.Replace(checkRow(row, column), "");
        numbers = numbers.Replace(checkColumn(row, column), "");
        numbers = numbers.Replace(checkBoxPos(row, column), "");

        if (numbers.Length == 1)
         {
            newTextBox.Text = numbers;
         }
    }

You need to check the value of newTextBox before using it. You need to check if the value inside newTextBox is null.

Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
  • I didn't downvote, but most likely because "try this {code dump}" isn't a helpful answer, as it doesn't explain what was wrong. – CodeCaster Aug 20 '14 at 10:16
  • @CodeCaster:- That makes sense. People can leave a comment like you after downvoting. Constructive criticizm is always good! Thanks – Rahul Tripathi Aug 20 '14 at 10:18