title: Object reference not set to an instance of an object - MySqlCommand
body: <p>I am making a application which should work with MySql database. I am getting this error, and I dont know why:</p>
  
  <pre><code>Object reference not set to an instance of an object
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>public void rtMean()
      {
          MySqlConnection conect = con.ConnectToMySQL();
          MySqlDataReader reader;
          MySqlDataReader readerMean;
  
          // OVERENIE, CI UZ STLPEC PRIEMER EXISTUJE
  
          string query1 = "SELECT * FROM feture";
          MySqlCommand cmd1 = new MySqlCommand(query1, conect);
          reader = cmd1.ExecuteReader();
  
          for (int j=0; j &lt; reader.FieldCount; j++)
          {
              if (reader.GetName(j) == "priemer")
              {
                  break;
              }
              else
              {
                  reader.Close();
                  cmd1.Cancel();
                  string queryAlterTable = "ALTER TABLE feture ADD COLUMN priemer FLOAT NOT NULL";
                  MySqlCommand cmd = new MySqlCommand(queryAlterTable, conect); //here I am getting the error
                  cmd.ExecuteScalar();
              }
          }
  </code></pre>
  
  <p>This should chceck if there is column "priemer" in one of my tables. If yes, it should end / break, and if no, my table should be altered with new column, named "priemer".</p>
  
  <p>Thank you for advices.</p>
  
  <p>EDIT: STACK - TRACE:</p>
  
  <pre><code>  at MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar()
    at WindowsFormsApplication1.ClusteringReady.rtMean() in    c:\Users\Martin\Desktop\CLUSTER\WindowsFormsApplication1\ClusteringReady.cs:line 43
    at WindowsFormsApplication1.Form1.clusterize_Click(Object sender, EventArgs e) in c:\Users\Martin\Desktop\CLUSTER\WindowsFormsApplication1\Form1.cs:line 130
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message&amp; m)
    at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
    at System.Windows.Forms.Button.WndProc(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
    at    System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at WindowsFormsApplication1.Program.Main() in c:\Users\Martin\Desktop\CLUSTER\WindowsFormsApplication1\Program.cs:line 19
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>