title: Object reference not set to an instance of an object C# class
body: <p>I keep getting this error while trying to call a DataAccess layer in my C# windows form application.</p>
  
  <p>In my design, I have used an interface and a class.</p>
  
  <p>The inteface called iDataAccess, and the code inside it looks like this:</p>
  
  <p>IDataAccess.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data;
  public interface IDataAccess
  {
  
      object GetScalar(string sql);
      DataTable GetDataTable(string sql);
      int InsOrUpdOrDel(string sql);
  }
  </code></pre>
  
  <p>The class called DataAccess and the code inside it looks something like this:
  DataAccess.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.SqlClient;
  using System.Data;
  using System.Configuration;
  
  public class DataAccess : IDataAccess  
  {
      public static string CONNSTR = ConfigurationManager.ConnectionStrings["SqlDbConnect"].ConnectionString;
  
      public DataAccess()
  
      {
  
      }
  
    public System.Data.DataTable GetDataTable(string sql)
      {
          DataTable dt = new DataTable();
          SqlConnection conn = new SqlConnection(CONNSTR);
          try
          {
              conn.Open();
              SqlDataAdapter da = new SqlDataAdapter(sql, conn);
              da.Fill(dt);
          }
          catch (Exception)
          {
              throw;
          }
          finally
          {
              conn.Close();
          }
          return dt;
      }
  }
  </code></pre>
  
  <p>Now, in form.cs code I want to create an object of the mentioned class. For this reason, I have tried two different ways, but none of them worked for me, as shown below:</p>
  
  <p><code>IDataAccess _idataAccess = new DataAccess();</code>    // I get instance failure error</p>
  
  <p><code>IDataAccess _idataAccess = null;</code>   // Error: Object reference not set to instance of an object</p>
  
  <p>can anyone give me an idea about this problem, please?</p>