vb.net 创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:未将对象引用设置为对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2866831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:53:36  来源:igfitidea点击:

An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object

vb.netformsobjectinstance

提问by Ben

I get this error when attempting to debug my form, I cannot see where at all the error could be (also does not highlight where), anyone have any suggestions?

尝试调试表单时出现此错误,我看不到错误可能在哪里(也没有突出显示位置),有人有什么建议吗?

An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:未将对象引用设置为对象的实例。

Dim dateCrap As String = "Date:"
Dim IPcrap As String = "Ip:"
Dim pcCrap As String = "Computer:"
Dim programCrap As String = "Program:"

Dim textz As String
Dim sep() As String = {vbNewLine & vbNewLine}
Dim sections() As String = Text.Split(sep, StringSplitOptions.None)

Dim NewArray() As String = TextBox1.Text.Split(vbNewLine)


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    textz = TextBox1.Text
End Sub

采纳答案by Konrad Rudolph

The error is here:

错误在这里:

Dim textz As String = TextBox1.Text

and here:

和这里:

Dim NewArray() As String = TextBox1.Text.Split(vbNewLine)

and possibly here:

可能在这里:

Dim sections() As String = Text.Split(sep, StringSplitOptions.None)

You cannot initialize a member like this because this code is basically executed in the constructor, beforeTextBox1(or any other control/property) is initialized, hence it is Nothing.

您不能像这样初始化成员,因为此代码基本上在构造函数中执行,TextBox1(或任何其他控件/属性)初始化之前,因此它是Nothing.

Put all initializations that refer to controls inside the Form_Loadevent – that's what it's there for.

将所有引用控件的初始化放在Form_Load事件中——这就是它的用途。

回答by Ankush

Turn off "Just MY Code" under debugging section on the "Options>General" tab. That'll show you where the exact error originates.

在“选项>常规”选项卡上的调试部分下关闭“仅我的代码”。这将向您显示确切错误的来源。

回答by John Glover

I had the same symptoms - couldn't even start debugging, as the error appeared before any of my code started to run. Eventually tracked it down to a resize event handler:

我有同样的症状 - 甚至无法开始调试,因为错误出现在我的任何代码开始运行之前。最终将其追踪到一个调整大小的事件处理程序:

Private Sub frmMain_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize

Private Sub frmMain_Resize(sender As Object, e As System.EventArgs) 处理 Me.Resize

ArrangeForm()

End Sub

结束子

As soon as I removed the handler, the error disappeared. The odd thing is that it had been running for about 3 weeks (while I developed other parts of the code) without any problem, and just spontaneously stopped working. A ResizeEnd event handler caused no problem.

一旦我删除了处理程序,错误就消失了。奇怪的是它已经运行了大约 3 周(当我开发代码的其他部分时)没有任何问题,只是自发地停止工作。ResizeEnd 事件处理程序没有引起任何问题。

Just posting this in case anyone else is unfortunate enough to encounter the same problem. It took me 8 hours to track it down.

只是张贴这个以防其他人不幸遇到同样的问题。我花了8个小时才找到它。