Question null reference?

Mirace

Member
Joined
Apr 29, 2012
Messages
9
Programming Experience
3-5
Hello forum.

Im working on threaded tcp server and i faced very strange problem and i can't find anything from google what can help me. Basicly im calling sub Send from module, it should work. But i get exception error and when i move my cursor to mS it says = Nothing . Where my object did go?

Here is code

problem1.png


VB.NET:
Public Class NetSession    Private Hash As String = Guid.NewGuid.ToString()
    Private PlayerList As New ArrayList()
    Public PlayerPassword As New ArrayList()
    Public PlayerUsername As New ArrayList()
    Private mC As System.Net.Sockets.TcpClient
    Public mS As System.Net.Sockets.NetworkStream
    Private mThreadCounter As Integer
    Private mIndex As String


    Public Sub New(ByVal C As System.Net.Sockets.TcpClient,
          ByVal ThreadCounter As Integer)
        Me.mC = C
        Me.mThreadCounter = ThreadCounter
        Me.mIndex = ""
    End Sub


    Public Sub New()
        ' TODO: Complete member initialization 
    End Sub


    Public Sub SessionLoop()
        Me.mS = Me.mC.GetStream()
        Me.mC.LingerState.Enabled = False
        Me.Send("0x00" & Hash)
        While Me.mC.Connected
            If Me.mS.DataAvailable Then
                Dim GotData As New System.Text.StringBuilder()
                While Me.mS.DataAvailable
                    Dim bytes(Me.mC.ReceiveBufferSize - 1) As Byte
                    Dim Len = Me.mS.Read(bytes, 0, Me.mC.ReceiveBufferSize)
                    If Len > 0 Then
                        GotData.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, Len))
                    End If
                End While
                Dim D = GotData.ToString().Trim()
                If Not D = "" Then
                    If Not Me.Handle(D) Then
                        Exit While
                    End If
                End If
            End If
        End While
        Me.mS.Close()
        Me.mS.Dispose()
        Me.mC.Close()
        Console.WriteLine("Thread " & Me.mThreadCounter.ToString() & " is closing.")
    End Sub


    Sub Send(ByVal T As String)


        Dim Bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(T & ControlChars.CrLf)
        Me.mS.Write(Bytes, 0, Bytes.Count)
        Me.mS.Flush()
    End Sub


    Public Function Handle(ByVal D As String) As Boolean
        Console.WriteLine("Thread " & Me.mThreadCounter.ToString() & ": " & D)
        If D.Substring(0, 4) = "FINN" Then
            Return False
        Else
            Select Case D.Substring(0, 4)
                Case "0x00" : Call ModPackets.Login0x00(D.Substring(4, D.Length - 4))
                Case "0x01" : Call ModPackets.Login0x01(D.Substring(4, D.Length - 4))
                Case Else
                    Console.WriteLine("Unparsed packet " + D.Substring(0, 4) + " Data: " + D.Substring(4, D.Length - 4))
            End Select


            Return True
        End If
    End Function


End Class
 
mS is assigned in SessionLoop, which also closes the connection. That you get a null reference mean it has never been assigned.
 
mS is assigned in SessionLoop, which also closes the connection. That you get a null reference mean it has never been assigned.


That means i have async connection? Should i remove "While Me.mC.Connected" while loop and get Syncronized system to work? My clients are Syncronized.

Thanks for reply, im waiting new :)
 
No, it means you have not assigned anything to mS before trying to use it.
 
No, it means you have not assigned anything to mS before trying to use it.

Okey.. but can you explain this: When my tcp client connects to server, server sends 0x00 , including unique hash. My client receives that NORMALY. But second time when my tcp does send something i get this error on server side.

Look packet log:

VB.NET:
Client connects to server..
OK!

Server send 0x00 with some data:
1  Hide  Hide  42  Send  0000  30 78 30 30 32 31 65 32 34 31 36 63 2D 35 31 63    0x0021e2416c-51c
0010  63 2D 34 63 33 38 2D 61 66 32 36 2D 62 33 66 38    c-4c38-af26-b3f8
0020  64 39 36 36 31 63 36 35 0D 0A                      d9661c65..

Client sending back username and password:
2  Hide  Hide  17  Recv  
0000  30 78 30 31 61 64 6D 69 6E 2F 31 32 33 2F 30 0D    0x01admin/123/0.
0010  0A

Then happens this :
problem1.png
 
Last edited:
As you said yourself in first post, mS is Nothing, either it was never assigned or some code set it to Nothing later. That is where you must look. You can't call mS.Write when mS is nothing.
 
Sorry, my english isn't best :) But yes i said that mS is nothing, but i said wrong. It goes Nothing by itself after it send first data. So,

first time when i use Send sub it works , second time i get that error AND i do not assing it nothing on my project. If i need to assign it, how i do it? i have tcpAccept loop on it's own thread and it launches Netsession.vb with own thread when user connects.

VB.NET:
Module Module1


    Sub StartServer()
        Console.WriteLine("Server allows now connections.")
        Dim L As New System.Net.Sockets.TcpListener(New System.Net.IPAddress({192, 168, 0, 103}), 7112)
        L.Start()
        Dim ThreadCounter = 0
        Do
            ThreadCounter += 1
            Dim C = L.AcceptTcpClient()
            Dim S As New NetSession(C, ThreadCounter)
            Dim T As New Threading.Thread(AddressOf S.SessionLoop)
            Console.WriteLine("Starting thread " & ThreadCounter.ToString() & ".")
            T.Start()
        Loop
    End Sub


End Module
 
It goes Nothing by itself after it send first data
That is not possible, a variable can't reset itself.
first time when i use Send sub it works , second time i get that error
There is only one call to Send, and that is third line in SessionLoop method. At that time mS has been assigned by first line in same method.
 
That is not possible, a variable can't reset itself.

There is only one call to Send, and that is third line in SessionLoop method. At that time mS has been assigned by first line in same method.


Thanks for help JohnH but problem isn't assign. I made big mistake.. as old vb 6 developer ofcourse i think i can use module in vb.net like in vb6.. I got it working. Thanks

What i did ?

I made new public class, writed some functions to return data and data is returned to sendbuffer. After that i made if statement to check if there is anything to send to client. I think main problem was that i was trying to send while socket was blocking.

Here is code
VB.NET:
    Public Function Handle(ByVal D As String) As Boolean
        Dim Packet As New Packets
        Console.WriteLine("Thread " & Me.mThreadCounter.ToString() & ": " & D)
        If D.Substring(0, 4) = "FINN" Then
            Return False
        Else
            Select Case D.Substring(0, 4)
                Case "0x00" : SendBuffer = Packet.Login0x00(D.Substring(4, D.Length - 4))
                Case "0x01" : SendBuffer = Packet.Login0x01(D.Substring(4, D.Length - 4))
                Case Else
                    Console.WriteLine("Unparsed packet " + D.Substring(0, 4) + " Data: " + D.Substring(4, D.Length - 4))
            End Select


            If (SendBuffer.Length > 1) Then
                Send(SendBuffer)
                SendBuffer = ""
            End If


            Return True
        End If
    End Function
 
Last edited:
Back
Top