发生了未处理的异常。你调用的对象是空的 [英] Unhandled exception has occurred. Object reference not set to an instance of an object

查看:126
本文介绍了发生了未处理的异常。你调用的对象是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Windows窗体应用程序的发行版.exe,我收到以下错误。这个问题奇怪地只发生在我尝试在生成的发布目录外运行.exe时?在visual studio中运行或在发布目录中运行.exe文件时,它可以正常工作。在以下错误之后,我已经包含了可疑代码。它似乎试图连接到数据库似乎是一个问题,但我不知道为什么





请参阅此消息的结尾有关调用

实时(JIT)调试而非此对话框的详细信息。



******** ******异常文本**************

System.NullReferenceException:对象引用未设置为对象的实例。
$ bForb at WindowsFormsApplication4.DataAccess.DataAccess.get_ConnectionString()

at WindowsFormsApplication4.DataAccess.DataAccess.AuthenitcateCredentials(String username,String password)

at WindowsFormsApplication4.LoginRegister.Login_Click (对象发送者,EventArgs e)

在System.Windows.Forms.Control.OnClick(EventArgs e)

在System.Windows.Forms.Button.OnClick(EventArgs e) )

在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

在System.Windows.Forms.Control.WmMouseUp(Message& m,Mouse System.Windows.Forms.Control.WndProc上的按钮按钮,Int32点击)

(消息& m)

在System.Windows.Forms.ButtonBase.WndProc(Message& m)

在System.Windows.Forms.Button.WndProc(Message& m)
(消息& m)

在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)





**************已加载的装配**************

mscorlib

汇编版本:4.0.0.0

Win32版本:4.0.30319.296(RTMGDR.030319-2900)

CodeBase:file:/// C:/Windows/Microsoft.NET /Framework/v4.0.30319/mscorlib.dll

-------------------------------- --------

WindowsFormsApplication4

汇编版本:1.0.0.0

Win32版本:1.0.0.0

CodeBase:file:/// C:/ Users / Admin / Documents / dt265 / Project / website / WindowsForm sApplication4.exe

--------------------------------------- -

System.Windows.Forms

汇编版本:4.0.0.0

Win32版本:4.0.30319.278建立者:RTMGDR

CodeBase:file:/// C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

----------------------------------------

System.Drawing

汇编版本:4.0.0.0

Win32版本:4.0.30319.282内置:RTMGDR

CodeBase:file:// /C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

----------- -----------------------------

系统

汇编版本:4.0 .0.0

Win32版本:4.0.30319.296内容:RTMGDR

CodeBase:file:/// C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/ v4.0_4.0.0.0__b77a5c561934e089 / System.dll

-------------- --------------------------

System.Data.Entity

汇编版本: 4.0.0.0

Win32版本:4.0.30319.1内容:RTMRel

CodeBase:file:/// C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System .Data.Entity / v4.0_4.0.0.0__b77a5c561934e089 / System.Data.Entity.dll

---------------------- ------------------

System.Core

汇编版本:4.0.0.0

Win32版本:4.0.30319.233内容:RTMGDR

CodeBase:file:/// C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089 /System.Core.dll

------------------------------------ ----

System.Data

汇编版本:4.0.0.0

Win32版本:4.0.30319.237(RTMGDR.030319-2300)

CodeBase:file:/// C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll

----------------------------------------

系统m.Configuration

汇编版本:4.0.0.0

Win32版本:4.0.30319.1(RTMRel.030319-0100)

CodeBase:file: ///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

--------- -------------------------------

System.Xml

汇编版本:4.0.0.0

Win32版本:4.0.30319.233内容:RTMGDR

CodeBase:file:/// C:/Windows/Microsoft.Net/assembly/ GAC_MSIL / System.Xml / v4.0_4.0.0.0__b77a5c561934e089 / System.Xml.dll

----------------------- -----------------



************** JIT调试* *************

启用实时(JIT)调试,这个

应用程序或计算机的.config文件(machine.config)必须在system.windows.forms部分设置

jitDebugging值。

应用程序也必须使用调试编译

已启用。


例如:



< configuration>

< system。 windows.forms jitdebugging =true>





启用JIT调试时,任何未处理的异常

将被发送到计算机上注册的JIT调试器

而不是由此对话框处理。







I am getting the below error for a release .exe of a windows form application i am building. The issue strangely only occurs when i try to run the .exe outside out of the release directory it was generated ?! It works fine when ran in visual studio or by running the .exe file in the release directory. After the below error i have included the suspect code. It appears trying to connect to the database seems to be a issue but i do not know why


See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsFormsApplication4.DataAccess.DataAccess.get_ConnectionString()
at WindowsFormsApplication4.DataAccess.DataAccess.AuthenitcateCredentials(String username, String password)
at WindowsFormsApplication4.LoginRegister.Login_Click(Object sender, EventArgs e)
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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.296 (RTMGDR.030319-2900)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
WindowsFormsApplication4
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/Admin/Documents/dt265/Project/website/WindowsFormsApplication4.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.278 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.282 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.296 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Data.Entity
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.Entity/v4.0_4.0.0.0__b77a5c561934e089/System.Data.Entity.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.233 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.237 (RTMGDR.030319-2300)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.233 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitdebugging="true">


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.



public class DataAccess
    {
        public BusinessObjects.User CurrentUser = new BusinessObjects.User();

        private string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["DatabaseEntities"].ToString(); }
        }

        public BusinessObjects.User AuthenitcateCredentials(string username, string password)
        {
            using(var dataContext = new DatabaseEntities(ConnectionString))
            {
                var user = dataContext.Users.FirstOrDefault(u => u.Username.Equals(username) && u.Password.Equals(password));
                if (user == null) return null;
                CurrentUser = GetUser(user.UserId);
                return CurrentUser;
            }
        }

推荐答案

您是否将AppConfig文件复制到了Exe文件夹?



,因为您正在使用配置管理器,您需要在可执行文件夹中构建应用程序时保留生成的AppConfig文件。
Did you copy the AppConfig file to your Exe folder?

since you are using the Configuration manager you need to keep the AppConfig file generated when you build the application inside the executable folder.


这篇关于发生了未处理的异常。你调用的对象是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆