C# 实例化调用new的行时出现NullReferenceException

C# 实例化调用new的行时出现NullReferenceException,c#,C#,以上代码使用csc.exe进行编译,但在运行时抛出NullReferenceException: 未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。 在AppHelper.ctor static void Main() { AppHelper helper = new AppHelper(); // more stuff } 调试器从app.config查找connectionString,但运行可执行文件阻塞。您在AppHe

以上代码使用csc.exe进行编译,但在运行时抛出NullReferenceException: 未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。 在AppHelper.ctor

static void Main()
{
    AppHelper helper = new AppHelper();
    // more stuff
}

调试器从app.config查找connectionString,但运行可执行文件阻塞。

您在AppHelper构造函数或其内联字段初始值设定项中有使用空引用的代码


如果您在调试器中运行代码,它在哪里中断?

您在AppHelper类的构造函数中执行任何操作吗?

代码看起来不错。问题出在其他地方,或者代码不同

我能想到的唯一一件事是,类中碰巧有内联初始值设定项

public class AppHelper
{
    private string connect = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString();

    public AppHelper()
    {
        // TODO
    }
}

需要将app.config复制到${exeName}.exe.config。这就是我在最后解决问题的方法。

他似乎并不是因为只有TODO评论。整个类声明是什么?堆栈跟踪中的行号是多少?在调试器中不会中断。只有在运行可执行文件时才会中断。私有字符串connect=System.Configuration.ConfigurationManager.connectionString[connectionString].ToString可能有问题;这就好像可执行文件找不到配置文件一样。但是,在编译时,我指示csc/appconfig:app.config。
public class AppHelper
{

   string something = null;
   int somethingLength = something.Length;

   public AppHelper()
   {
      // TODO
   }