加载表单时出错 [英] Error while Form loading

查看:194
本文介绍了加载表单时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,
我正在Visual Studio 2010中开发一个VB应用程序.在加载表单时出现错误.当我单击button4时,我需要隐藏当前表单并显示下一个表单,但是我得到了
错误消息:对象引用未设置为对象的实例"
这是我的代码.....

Hai All,
I am developing one VB application in Visual Studio 2010. In that i got an error while load form. When i click button4 i need to hide current form and show next form, but i get an
Error Message: "OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT"
Here''s my code .....

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As            System.EventArgs) Handles Button4.Click
    Try
        Dim frm As New SMSQuire
        Hide()
        frm.ShowDialog()
    Catch EX As Exception
        MsgBox(EX.Message)
    End Try
End Sub



请帮助我克服此错误...



Please help me to overcome this error......

推荐答案

即使您发现了问题,也肯定需要在这里提供帮助:
该异常是使用调试器发现的最简单的,不需要询问的异常之一.您取消引用空引用,因此您需要先检查变量/成员是否为空,而不在下一行中使用它,或确保变量/成员已初始化为对某个对象的引用(可以创建或不创建),然后再进行引用取消引用.

第二件事:在事件上捕获异常是对异常机制的滥用.异常应该捕获在每个线程的堆栈顶部,并且在许多情况下,只能在一个位置(每个线程)捕获异常.在UI中,它们也应该陷入UI线程的主循环中.对于Forms,这是通过重写System.Windows.Forms.Application.OnThreadException或处理事件System.Windows.Forms.Application.ThreadException来完成的.请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception.aspx [ ^ ].

仅当您将正确的未处理异常模式设置为System.Windows.Forms.UnhandledExceptionMode.CatchException的值时,此功能才起作用.请参阅:
http://msdn.microsoft.com/en-us/library/ms157905.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.unhandledexceptionmode.aspx [ ^ ].

希望对您有所帮助.

—SA
Even though you found the problem, you certainly need help here:
This exception is one of the easiest, never needs asking a question, found by using the debugger. You de-reference null reference, so you need to either check a variable/member for null and not use it in next line or make sure the variable/member is initialized to a reference to some object (which can be created or not) before de-referencing it.

Second thing: catching exception on event is the abuse of the exception mechanism. Exceptions should be caught on top of stack of each thread, and, in many cases, only in one place (per thread). In UI, they also should be caught in the main cycle of UI thread. With Forms, this is done by overriding System.Windows.Forms.Application.OnThreadException or handling the event System.Windows.Forms.Application.ThreadException. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception.aspx[^].

This functionality will only work if you set proper unhandled exception mode — to the value of System.Windows.Forms.UnhandledExceptionMode.CatchException. Please see:
http://msdn.microsoft.com/en-us/library/ms157905.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.unhandledexceptionmode.aspx[^].

Hope it will help you a bit.

—SA


这篇关于加载表单时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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