Hello,
I have some server code to accept a TCP connection from another computer and to display a message in a listbox on the server.
"Receiver" is the name of a backgroundworker.
I've only displayed relevant code here:
Private Sub Receiver_DoWork(ParametersWereHere) Handles Receiver.DoWork
Try
Dim TCPListener As New TcpListener(IncomingPort)
TCPListener.Start()
While True
client = New ClientConnection(TCPListener.AcceptTcpClient, AddressOf ListBoxUpdater)
End While
Catch ex As Exception
MsgBox("3. " & ex.Message)
End Try
End Sub
Private Sub ListBoxUpdater(ByVal WhatToWrite As String)
If Me.InvokeRequired Then
***** Me.Invoke(client.UpdateListBox, WhatToWrite) *****
Else
Me.ListBox2.Items.Add(WhatToWrite)
End If
End Sub
And in the class ClientConnection:
Public Delegate Sub UpdateListInvoker(ByVal WhatToWrite As String)
Public UpdateListBox As UpdateListInvoker
Public Sub New(ByVal Client As TcpClient, ByRef _callback As UpdateListInvoker)
UpdateListBox = _callback
TCPClient = Client
ClientIP = Client.Client.RemoteEndPoint.ToString
AllClients.Add(ClientIP, Me)
UpdateListBox("Connected: " & ClientIP)
ReDim data(TCPClient.ReceiveBufferSize)
TCPClient.GetStream.BeginRead(data, 0, CInt(TCPClient.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
End Sub
The problem I'm facing is that on the starred line in the first code block, I get the error
object reference not set to an instance of an object
when I comment out the "try... catch... end try" statement in Receiver_DoWork().
The Watch window showed that "client" was empty, but it should have a ClientConnection assigned to it.
Can anyone see what's wrong here?
-Rixterz