'System.NullReferenceException' 在初始化 ConnectionManager 时抛出

'System.NullReferenceException' thrown while initializing ConnectionManager

我正在尝试在

中初始化 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 was unhandled HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=WindowsFormsApplication1 StackTrace: at WindowsFormsApplication1.ShowVoc..ctor() in C:\Users\user\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\ShowVoc.cs:line 24 at WindowsFormsApplication1.main.voc_Click_1(Object sender, EventArgs e) in C:\Users\user\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\main.cs:line 53 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& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 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\user\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) at System.Activator.CreateInstance(ActivationContext activationContext) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 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() InnerException:

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

的引用

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

public ShowVoc()
        { string connectionString="";
            InitializeComponent();

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

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

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

不就是连接字符串的key打错了吗?我看到您在末尾添加了分号,XML 中没有分号。去掉应该没问题。