An error occurred creating the form. See Exception

2019-06-28 00:22发布

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.

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

3条回答
2楼-- · 2019-06-28 00:40

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, before TextBox1 (or any other control/property) is initialized, hence it is Nothing.

Put all initializations that refer to controls inside the Form_Load event – that’s what it’s there for.

查看更多
SAY GOODBYE
3楼-- · 2019-06-28 00:42

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

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.

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

查看更多
【Aperson】
4楼-- · 2019-06-28 00:51

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

查看更多
登录 后发表回答