vb.net Visual Basic System.invalidOperationexception

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17660944/
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-17 14:15:47  来源:igfitidea点击:

Visual Basic System.invalidOperationexception

vb.net

提问by Gabe

so I get this error saying that I get a System.invalidOperationexception here is the full error:

所以我得到这个错误,说我得到一个 System.invalidOperationexception 这是完整的错误:

[Managed to Native Transition]
Port Scan.exe!WindowsApplication1.My.MyProject.MyForms.Form1.get()
Port Scan.exe!WindowsApplication1.My.MyApplication.OnCreateMainForm()
    Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicatio Base.OnRun()
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplication    Base.DoApplicationModel()
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplication    Base.Run(string[] commandLine)
[Native to Managed Transition]
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationCo    ntext activationContext, string[] activationCustomData)
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.H    ostProc.RunUsersAssemblyDebugInZone()
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContex   t executionContext, System.Threading.ContextCallback callback, object state, bool      preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext   executionContext, System.Threading.ContextCallback callback, object state, bool  preserveSyncCtx)
 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext     executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
[Native to Managed Transition]

and here is the code I am trying to compile,it for a port scanner

这是我正在尝试编译的代码,它用于端口扫描器

Public Class Form1
Dim host As String

Dim counter As Integer
Dim portmin As Integer = TextBox3.Text
Dim portmax As Integer = TextBox2.Text
Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    Button1.Enabled = False
    'set counter explained before to 0
    counter = 0
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Timer1.Tick
    'Set the host and port and counter
    counter = counter + 1 'counter is for the timer
    host = TextBox1.Text



    For port As Integer = portmin To portmax

        If (port = portmax) Then
            Exit For
        End If



        ' Next part creates a socket to try and connect 
        ' on with the given user information.

        Dim hostadd As System.Net.IPAddress = _
            System.Net.Dns.GetHostEntry(host).AddressList(0)
        Dim EPhost As New System.Net.IPEndPoint(hostadd, port)
        Dim s As New System.Net.Sockets.Socket( _
      System.Net.Sockets.AddressFamily.InterNetwork, _
    System.Net.Sockets.SocketType.Stream, _
      System.Net.Sockets.ProtocolType.Tcp)
        Try
            s.Connect(EPhost)
        Catch
        End Try
        If Not s.Connected Then
            ListBox1.Items.Add("Port " + port.ToString + " is not open")
        Else
            ListBox1.Items.Add("Port " + port.ToString + " is open")
            ListBox2.Items.Add(port.ToString)

        End If
        Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString
    Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    'stop button
    Timer1.Stop()
    Timer1.Enabled = False
    Button1.Enabled = True
    Button2.Enabled = False
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
    ListBox1.Items.Add("Scanning: " + TextBox1.Text)
    ListBox1.Items.Add("-------------------")
    Button2.Enabled = True
    Button1.Enabled = False
    Timer1.Enabled = True
    Timer1.Start()
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged

End Sub
End Class

I really appreciate your help as I have been stuck on this error for some time and can't build the exe. It seems the error only happens when I do a for loop to allow minimum and maximum port number.

我真的很感谢你的帮助,因为我已经被这个错误困扰了一段时间并且无法构建 exe。似乎错误仅在我执行 for 循环以允许最小和最大端口号时发生。

回答by Joel Coehoorn

The problem is in this code:

问题出在这段代码中:

Dim portmin As Integer = TextBox3.Text
Dim portmax As Integer = TextBox2.Text

At the time those statements execute, the form is not fully constructed yet. TextBox2and TextBox3have not yet been created. You're trying to assign a reference Nothingto an integer value. This is not allowed. Move the assignments to your form's Load event. Also take the time to check and make sure the contents of those boxes will convert to an integer.

在执行这些语句时,表单尚未完全构建。TextBox2并且TextBox3尚未创建。您正在尝试分配Nothing对整数值的引用。这是不允许的。将分配移至表单的 Load 事件。还要花时间检查并确保这些框的内容将转换为整数。