运行时异常:-对象引用未设置为对象的实例 [英] Runtime Exception :- Object Reference not set to an instance of an object

查看:168
本文介绍了运行时异常:-对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#2.0中开发窗口应用程序,所以此应用程序非常大,而且我正在开发一个模块.

运行时的例外是:-
对象引用未设置为对象的实例".

我感到困惑,因为它是从哪里生成的,为什么要生成它,因为解决方案在编译时没有错误,并且数据也保存到数据库中,但是单击该数据的结果按钮时无法获取.



请帮帮我.
谢谢!

i m working on window application in C# 2.0 this application is so large and i m working on one module.

the exception at run time is:-
"Object Reference not set to an instance of an object".

i m confuse, from where it is generate and why it is generated, because the solution has no error at compile time and also the data is saved to the database but could not fetch when click on the result button for that data.



Please help me.
thanks!

推荐答案

您是否已在调试器下运行应用程序?它是Visual Studio附带的免费工具.
Have you run your app under the debugger yet? It''s a free tool that comes with Visual Studio.


确定,因此,在结果按钮"单击事件处理程序中添加一个断点.现在逐步检查代码,直到出现错误

编译时错误和运行时错误是完全不同的东西!
OK, so add a breakpoint to the ''result button'' click event handler. Now step through your code until the error occurs

Compile time and Run time errors are very different things!


当您尝试使用未设置为对象有效实例的引用时,即会发生这种情况.空引用.

当您希望在调试器下运行程序时,当您希望跟踪空引用和其他异常时,以下内容可能会有所帮助.当您使用他人编写的代码并试图弄清楚问题时,它特别有用:)

AppDomain有两个有趣的事件:
AppDomain.FirstChanceException事件 [ AppDomain.UnhandledException事件 [
This it what happen when you try to use a reference that is not set to a valid instance of an object - ie. a null reference.

The following may be helpful when you wish to track down null references, and other exceptions, when running a program under a debugger. It''s particularly useful when you are working with code written by others, and trying to figure things out :)

AppDomain has two interesting events:
AppDomain.FirstChanceException Event[^]
and
AppDomain.UnhandledException Event[^]

So you can write
static void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e)
{
#if DEBUG
 if (System.Diagnostics.Debugger.IsAttached)
 {
  System.Diagnostics.Debugger.Break();
 }
#endif
}



然后在您的Main方法中分配处理程序



And then assign the handler in your Main method

static void Main()
{
 AppDomain.CurrentDomain.FirstChanceException += FirstChanceHandler;
 // the rest of the Main method ...
}



问候
Espen Harlinn



Regards
Espen Harlinn


这篇关于运行时异常:-对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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