Vb.net Visual Basic System.invalidOperationexception
所以我得到这个错误,说我得到一个System.invalidOperationexception 以下是全部错误:Vb.net Visual Basic System.invalidOperationexception,vb.net,Vb.net,所以我得到这个错误,说我得到一个System.invalidOperationexception 以下是全部错误: [Managed to Native Transition] Port Scan.exe!WindowsApplication1.My.MyProject.MyForms.Form1.get() Port Scan.exe!WindowsApplication1.My.MyApplication.OnCreateMainForm() Microsoft.VisualBasi
[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]
这是我试图编译的代码,它是为端口扫描器编写的
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
我真的很感谢你的帮助,因为我已经被这个错误困扰了一段时间,无法构建exe。似乎只有在执行for循环以允许最小和最大端口号时才会发生错误。问题在于以下代码:
Dim portmin As Integer = TextBox3.Text
Dim portmax As Integer = TextBox2.Text
在执行这些语句时,表单尚未完全构造<代码>文本框2和
文本框3
尚未创建。您正在尝试将引用Nothing
分配给整数值。这是不允许的。将分配移动到窗体的加载事件。还要花点时间检查并确保这些框的内容将转换为整数。问题出在以下代码中:
Dim portmin As Integer = TextBox3.Text
Dim portmax As Integer = TextBox2.Text
在执行这些语句时,表单尚未完全构造<代码>文本框2和
文本框3
尚未创建。您正在尝试将引用Nothing
分配给整数值。这是不允许的。将分配移动到窗体的加载事件。还要花时间检查并确保这些框的内容将转换为整数。portmin(TextBox3.Text
)和portmax
(TextBox2.Text
)的值是多少?portmin
(TextBox3.Text
)和portmax
(TextBox2.Text
)?不太确定“将其移动到加载事件”这是一个很好的建议。用户仍然没有时间输入值。而且,更糟糕的故障模式是,加载事件处理程序中的异常往往会被吞没。最好使用开始按钮。感谢您的超快速操作answer@Gabe-此外,进入项目属性并启用Option Strict或在代码顶部启用Option Strict。而不是因此,请确保“将其移动到加载事件”这是一个很好的建议。用户仍然没有时间输入值。而且,更糟糕的故障模式是,加载事件处理程序中的异常往往会被吞没。最好使用开始按钮。感谢您的超快速操作answer@Gabe-此外,进入项目属性并启用Option Strict或在代码顶部启用Option Strict。