1

我有一个DAL以连接到我的database. 当我创建一个新项目时,我总是继续Add Existing Item添加这个类。一直工作!但是现在我导入了我的新项目,我在标题上遇到了那个错误......我希望你们中的一些人能向我解释一下。
我知道当方法返回 NULL 值时会发生此异常。(也许不是)。

这是我的代码:

#region Singleton
  public sealed class Singleton
    {
       static readonly DAL instance = new DAL(); //HERE IS WHERE THE EXCEPTION OCCURS

       // Explicit static constructor to tell C# compiler
       // not to mark type as beforefieldinit

  public static DAL Instance
    {
       get
          {
             return instance;
          }
     }
}
#endregion  

这就是我称之为这个类的地方。

namespace BD
{
    public class BDAssociado
    {
        private DAL _dal;
        public BDAssociado()
        {
            _dal = DAL.Singleton.Instance;

        }
     }
}  

达尔构造函数:

public DAL()
  {
   _Conexao = new MySqlConnection(ConfigurationManager.ConnectionStrings["NameOfConnectionString"].ConnectionString);
  }
4

2 回答 2

2

null在返回之前检查它是否是:

public static DAL Instance
    {
        get
          {
             if (instance == null) 
             {
                 instance = new DAL();
             }
             return instance;
          }
     }
于 2013-05-24T12:22:10.810 回答
2

检查配置文件中是否存在连接字符串键。

于 2013-05-24T13:46:06.243 回答