在client.exe中发生错误“system.invalidoperationexception” [英] Getting error "system.invalidoperationexception' occurred in client.exe"

查看:78
本文介绍了在client.exe中发生错误“system.invalidoperationexception”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我对visual basic非常陌生,并尝试使用TCP / IP通信创建应用程序来控制仪器。所有的编码都是通过在线资源提供的大量帮助完成的,但现在由于错误我在Client.exe中发生类型'System.InvalidOperationException'的未处理异常而停留在最后阶段



附加信息:创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:值不能为空。



参数名称:输入



有人可以帮我解决这个问题吗?以下是我的代码:



Imports System.Net.Sockets

Imports System.IO



Public Class Form1

'私人客户端作为TCPControl

私人消息作为字符串



公开客户端作为新的TcpClient





Dim readData As New BinaryReader(Stream)

Dim writeData As New BinaryWriter( Stream)

Dim Stream As NetworkStream = Client.GetStream()





Private Sub cmdConnect_Click(sender As对象,e作为EventArgs)处理cmdConnect.Click

'客户端=新TCPControl(169.254.22.165,3602)

Client.Connect(169.254.164.61, 3602)

如果Client.Client.Connected则cmdConnect.Text =已连接

'Dim stream As NetworkStream = Client.getStream()

结束子



私有子cmdSend_Click(发件人为对象,e为EventArgs)H andles cmdSend.Click

'SendMessage()



writeData.Write(MSTART)

'txtMessage .Clear()

'txtReceive.Clear()

txtMessage.Focus()

结束子

<如果Client.Client.Connected = True则Client.Send(txtMessage.Text)

'End Sub



私有子Form1_FormClosing(发件人为对象,e为FormClosingEventArgs)处理Me.FormClosing

如果Client.Client.Connected = True则< br $>
'Client.DataStream.Close()

Client.Client.Close()

结束如果

End Sub



私有子cmdReceive_Click(发件人为对象,e为EventArgs)处理cmdReceive.Click

Dim Astring As String

'tcpClient_DataArrival(消息)

'txtReceive。 AppendText()

'txtMessage.Clear()

Astring = readData.ReadString()

txtReceive.Text = Astring







End Sub



'Private Sub ReceiveMessage( )

'昏暗的消息为字符串

'如果Client.Client.Connected = True则Client.Receive(txtMessage.Text)

'客户端.Receive(消息)

'End Sub



'Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)

'Dim message As String

'Call Client.Receive(message)'从服务器获取数据

'txtReceive.Text = txtReceive.Text&消息& vbCrLf& vbCrLf

'txtReceive.SelectionStart = Len(txtReceive.Text)

'End Sub

结束班



我尝试了什么:



我尝试了不同的TCP / IP客户端服务器连接示例,但没有一个是按照我的申请工作。


用Putty检查
i,我可以使用Putty发送的命令控制仪器。

解决方案

首先,在代码中添加Try和Catch,错误看起来像对象引用错误。在某些时候客户端对象变为null。



如果你能解释一下你收到错误的话会更容易。


看看这段代码:

  Dim  readData  As   BinaryReader(Stream)
Dim writeData As BinaryWriter(Stream)
Dim Stream 作为 NetworkStream = Client.GetStream()





您正在使用名为Stream的变量(a顺便说一句坏名字!)创建BinaryReader和BinaryWriter的实例。问题是你必须在创建BinaryReader / Writer之前尝试使用它来定义该变量:

  Dim  Stream  As  NetworkStream = Client.GetStream()
Dim readData As BinaryReader(Stream)
Dim writeData As BinaryWriter(Stream)


Hello,

I am very new to visual basic and trying to make an application to control an instrument using TCP/IP communication. All the coding is done with lot of help from online resources but now i am stuck at final stage due to error "An unhandled exception of type 'System.InvalidOperationException' occurred in Client.exe

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Value cannot be null.

Parameter name: input"

Can someone help me out to solve this problem? Below is my code:

Imports System.Net.Sockets
Imports System.IO

Public Class Form1
'Private Client As TCPControl
Private Msg As String

Public Client As New TcpClient


Dim readData As New BinaryReader(Stream)
Dim writeData As New BinaryWriter(Stream)
Dim Stream As NetworkStream = Client.GetStream()


Private Sub cmdConnect_Click(sender As Object, e As EventArgs) Handles cmdConnect.Click
'Client = New TCPControl("169.254.22.165", 3602)
Client.Connect("169.254.164.61", 3602)
If Client.Client.Connected Then cmdConnect.Text = "Connected"
'Dim stream As NetworkStream = Client.getStream()
End Sub

Private Sub cmdSend_Click(sender As Object, e As EventArgs) Handles cmdSend.Click
'SendMessage()

writeData.Write("MSTART")
'txtMessage.Clear()
' txtReceive.Clear()
txtMessage.Focus()
End Sub

'Private Sub SendMessage()
'If Client.Client.Connected = True Then Client.Send(txtMessage.Text)
' End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Client.Client.Connected = True Then
' Client.DataStream.Close()
Client.Client.Close()
End If
End Sub

Private Sub cmdReceive_Click(sender As Object, e As EventArgs) Handles cmdReceive.Click
Dim Astring As String
' tcpClient_DataArrival(Message)
'txtReceive.AppendText()
'txtMessage.Clear()
Astring = readData.ReadString()
txtReceive.Text = Astring



End Sub

'Private Sub ReceiveMessage()
'Dim message As String
'If Client.Client.Connected = True Then Client.Receive(txtMessage.Text)
'Client.Receive(message)
'End Sub

' Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
'Dim message As String
'Call Client.Receive(message) ' get data from server
'txtReceive.Text = txtReceive.Text & message & vbCrLf & vbCrLf
'txtReceive.SelectionStart = Len(txtReceive.Text)
'End Sub
End Class

What I have tried:

I have tried different examples of TCP/IP client server connection but none is working as per my application.

i checked with Putty and i am able to control the instrument using commands sent in Putty.

解决方案

First, add Try and Catch in your code and the error looks like object ref error. At some point the client object getting null.

It will be easier if you can explain at which point you are getting an error.


Look at this block of code:

Dim readData As New BinaryReader(Stream)
Dim writeData As New BinaryWriter(Stream)
Dim Stream As NetworkStream = Client.GetStream()



You're using a variable called "Stream" (a bad name by the way!) to create an instance of a BinaryReader and BinaryWriter. The problem is that you have to define that variable FIRST before you try to use it in the creation of the BinaryReader/Writer:

Dim Stream As NetworkStream = Client.GetStream()
Dim readData As New BinaryReader(Stream)
Dim writeData As New BinaryWriter(Stream)


这篇关于在client.exe中发生错误“system.invalidoperationexception”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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