title: Taking values into an array of textboxes (ASP.NET using C#)
body: <p>So I have an array of textboxes dynamically appear (The number of textboxes depends upon how a number from a database). They draw to the screen just fine.</p>
  
  <pre><code>    i = 0;
  
      while (i &lt; size)
      {
          pnlTxtBoxes.Controls.Add(labels[i]);
          pnlTxtBoxes.Controls.Add(txtBoxes[i]);
          pnlTxtBoxes.Wrap = true;
          i++;
      }
  </code></pre>
  
  <p>Like I said, the textboxes appear and the labels are displaying correctly. But when I go to retrieve the text from them, I get the error "Object reference not set to an instance of an object."</p>
  
  <pre><code>    i = 0;
  
      while (i &lt; size)
      {
          values[i] = txtBoxes[i].Text;
          txtBoxes[i].Visible = false;
          labels[i].Visible = false;
          i++;
      } 
  </code></pre>
  
  <p>Does anybody have an idea as to why I'm getting this error (and what I can do to fix it)?</p>
  
  <p><strong>EDIT: Here is all of the code. This is just a development DB, so I am not worried about showing the password</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using MySql.Data.MySqlClient;
  
  public partial class dieClearanceCalc : System.Web.UI.Page
  {
  static string connectionString = "database=localhost;database=matedevdb;uid=dev;pwd=123;";
  MySqlConnection con = new MySqlConnection(connectionString);
  MySqlCommand cmd = new MySqlCommand("SELECT shapeName FROM tblShapes;");
  MySqlDataReader reader;
  int size;
  TextBox[] txtBoxes;
  Label[] labels;
  
  protected void Page_Load(object sender, EventArgs e)
  {
      cmd.Connection = con;
      try
      {
          con.Open();
          reader = cmd.ExecuteReader();
          while (reader.Read())
          {
              shapeSelection.Items.Add(reader.GetString(0));
          }
          reader.Close();
      }
      catch (Exception ex)
      {
          Response.Write("&lt;p style='Color:red'&gt;Error:&lt;br/&gt;" + ex + "&lt;/p&gt;");
      }
  }
  protected void shapeSelected(object sender, EventArgs e)
  {
      string[] labelTxt;
      int i = 0;
  
      //Make current elements invisable
      lblShape.Visible = false;
      shapeSelection.Visible = false;
      btnSelectShape.Visible = false;
  
      // find the size of the arrays
      cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
      reader = cmd.ExecuteReader();
      if (reader.Read())
      {
          size = reader.GetInt32(0);
      }
      reader.Close();
  
      labelTxt = new string[size];
      labels = new Label[size];
      txtBoxes = new TextBox[size];
  
      // gather the labels from the db
      cmd.CommandText = "SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
      reader = cmd.ExecuteReader();
  
      i = 0;
  
      while (reader.Read())
      {
          labelTxt[i] = reader.GetString("varDesc");
          i++;
      }
      reader.Close();
  
      i = 0;
  
      while (i &lt; size)
      {
          labels[i] = new Label();
          txtBoxes[i] = new TextBox();
          labels[i].Text = labelTxt[i];
          i++;
      }
  
      i = 0;
  
      while (i &lt; size)
      {
          pnlTxtBoxes.Controls.Add(labels[i]);
          pnlTxtBoxes.Controls.Add(txtBoxes[i]);
          pnlTxtBoxes.Wrap = true;
          i++;
      }
  
      btnSendData.Visible = true;
      //Response.Write(size); test to see if the size variable is working
      Response.Write(size);
  
  }
  
  protected void calc(object sender, EventArgs e)
  {
      //declarations  
      formula diagonal, periphery;
      string dFormula = "", pFormula = "";
      string[] variables;
      string[] values;
      int i = 0;
      //end of declarations
  
      // This value must be retrievd again, because somewhere size is getting a value of  0
      cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
      reader = cmd.ExecuteReader();
      if (reader.Read())
      {
          size = reader.GetInt32(0);
      }
      reader.Close();
  
  
      variables = new string[size];
      values = new string[size];
  
      i = 0;
  
      while (i &lt; size)
      {
          values[i] = txtBoxes[i].Text;
          txtBoxes[i].Visible = false;
          labels[i].Visible = false;
          i++;
      }
  
      btnSendData.Visible = false;
  
      // retrieve the diagonal formula from the db
      cmd.CommandText = "SELECT diagonalFormula, peripheryFormula FROM tblShapes WHERE shapeName='" + shapeSelection.SelectedValue + "'";
      reader = cmd.ExecuteReader();
      while (reader.Read())
      {
          dFormula = reader.GetString("diagonalFormula");
          pFormula = reader.GetString("peripheryFormula");
      }
      reader.Close();
  
      Response.Write(size);
  
      // gather the variable names from the db
      cmd.CommandText = "SELECT varName FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
      reader = cmd.ExecuteReader();
  
      while (reader.Read())
      {
          variables[i] = reader.GetString("varName");
          i++;
      }
      reader.Close();
  
      con.Close();
  
      diagonal = new formula(dFormula, variables, values);
      periphery = new formula(pFormula, variables, values);
  
      txtDiagonal.Visible = true;
      txtPeriphery.Visible = true;
  
      txtDiagonal.Text = diagonal.getEquation();
      txtPeriphery.Text = periphery.getEquation();
  }
  
  public static double Evaluate(string expression)
  {
      System.Data.DataTable table = new System.Data.DataTable();
      table.Columns.Add("expression", string.Empty.GetType(), expression);
      System.Data.DataRow row = table.NewRow();
      table.Rows.Add(row);
      return double.Parse((string)row["expression"]);
  }  
  </code></pre>
  
  <p>}</p>