【问题标题】:'System.NullReferenceException' thrown while initializing ConnectionManager初始化 ConnectionManager 时引发“System.NullReferenceException”
【发布时间】:2017-01-16 12:04:55
【问题描述】:

我正在尝试在

中初始化 ConnectionManager
 public ShowVoc()
            {
                InitializeComponent();

                connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString;"].ConnectionString;
            }

但每当我运行它时都会给我“System.NullReferenceException”

这是应用配置代码

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="WindowsFormsApplication1.Properties.Settings.splaceConnectionString"
                connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\splace.mdf;Integrated Security=True"
                providerName="System.Data.SqlClient" />
            <add name="WindowsFormsApplication1.Properties.Settings.VocConnectionString"
                connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Voc.mdf;Integrated Security=True"
                providerName="System.Data.SqlClient" />
        </connectionStrings>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
    </configuration>

这是异常详情

System.NullReferenceException 未处理 HResult=-2147467261
Message=对象引用未设置为对象的实例。
源 = WindowsFormsApplication1 堆栈跟踪: 在 C:\Users\user\Documents\Visual Studio 中的 WindowsFormsApplication1.ShowVoc..ctor() 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\ShowVoc.cs:line 24 在 C:\Users\user\Documents\Visual Studio 中的 WindowsFormsApplication1.main.voc_Click_1(Object sender, EventArgs e) 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\main.cs:line 53 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 原因, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 C:\Users\user\Documents\Visual Studio 中的 WindowsFormsApplication1.Program.Main() 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.Runtime.Hosting.ManifestRunner.Run(布尔 checkAptModel) 在 System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext 激活上下文,字符串 [] 激活自定义数据) 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext 激活上下文) 在 System.Activator.CreateInstance(ActivationContext 激活上下文) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调, 对象状态, Boolean 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:

PS:我已经添加了对 System.configration 的引用

【问题讨论】:

    标签: c# database visual-studio nullreferenceexception


    【解决方案1】:

    在您的连接字符串中,我看到了一个不需要的 ;在末尾。我认为你不需要那个。正确的版本应该是:

    connectionString = ConfigurationManager
        .ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString"]
        .ConnectionString;
    

    【讨论】:

    • 谢谢兄弟,这是一个非常愚蠢的举动,我没有注意到;我会删除这个问题,因为我认为它不值得。
    • 没问题。有时这些是最难注意到的事情,因为您会考虑除此之外的任何其他事情。我很高兴能够提供帮助。 :)
    【解决方案2】:

    以后,请在提出新问题之前使用 Google 和 SO 自己的搜索引擎搜索您的问题。 System.NullReferenceException 到处都有非常详细的解释。要解决您的问题,请更改为

    public ShowVoc()
            { string connectionString="";
                InitializeComponent();
    
                connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.splaceConnectionString;"].ConnectionString;
            }
    

    【讨论】:

      【解决方案3】:

      不就是连接字符串的key有错别字吗?我看到您在末尾添加了一个分号,它在 XML 中不存在。删除应该没问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多