0

Possible Duplicate:
NullReferenceException on instanciated object?
What is a NullReferenceException in .NET?

I have built a TCP client in C#, when the client cannot connect Im getting the below. I keep getting a NullReference Exception with the following code, I cannot figure how to catch it or stop it.. any help will be greatly appreciated.

Its happening at "int a = sReponseData.Length"

    {



        string sMesg = "LogOn Ext:" + txtdevice.Text;
        SendMessage(sMesg);


        int a = sResponseData.Length;


        //Geting the Script out of the message.

        if (a > 10)
        {
            string stemp = sResponseData;
            string[] sSplit = stemp.Split(new Char[] { ';'});
            foreach (string s in sSplit)
            {

                if ((s.Trim() != "Tag") & (s.Trim() != "StopStart"))
                    sScript = s;
            }
        }
     }

}

and here is the sMesg

    {
        InitializeComponent();
        try
        {
            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer 
            // connected to the same address as specified by the server, port
            // combination.
            //Int32 port = 13000;
            TcpClient client = new TcpClient("127.0.0.1", 32111);

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(sMsg);

            // Get a client stream for reading and writing.
            //  Stream stream = client.GetStream();

            NetworkStream stream = client.GetStream();

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);

            //MessageBox.Show("Sent: {0}" + sMsg);

            // Receive the TcpServer.response.


            // String to store the response ASCII representation.
            sResponseData = String.Empty;

            if (stream.CanRead)
            {
                byte[] myReadBuffer = new byte[1024];
                StringBuilder myCompleteMessage = new StringBuilder();
                int numberOfBytesRead = 0;

                // Incoming message may be larger than the buffer size.
                do
                {
                    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);

                    myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

                }
                while (stream.DataAvailable);

                // Print out the received message to the console.
                sResponseData = myCompleteMessage.ToString();
                //MessageBox.Show(sResponseData);
            }
            else
            {
                sResponseData = ("3");
                MessageBox.Show("TagIt Server is unavalible at this moment.");
            }

            // Close everything.
            stream.Close();
            client.Close();
        }
        catch (ArgumentNullException e)
        {
            WriteLog("ArgumentNullException: {0}" + e);
            MessageBox.Show("TagIt Server is unavalible at this moment.");
        }
        catch (SocketException e)
        {
            WriteLog("SocketException: {0}" + e);
            MessageBox.Show("TagIt Server is unavalible at this moment.");
        }

    }
Community
  • 1
  • 1
  • 2
    At what line in the code do you get the exception? Read the entire exception message. – Klaus Byskov Pedersen Feb 12 '12 at 23:41
  • 1
    The only thing that can be null on that line is `sResponseData`. So I'm guessing the second function never gets called, but it's impossible to know without getting more information. Fire up the debugger and verify whether sResponseData is, in fact, null where you're getting the error, your second function to see if it gets called, and if not, whichever function calls that to see why it's skipped, etc. – millimoose Feb 12 '12 at 23:42

2 Answers2

0

You can check if Response data has value or not:

Something like:

 int a;
 try
 {
 if(sResponseData==null || sResponseData=="" )
  {
  MessageBox.Show("ResponseData is NULL or Empty"); //Shows Error 
  }
  else
  {
    //SresponseData has value
    string sMesg = "LogOn Ext:" + txtdevice.Text;
    SendMessage(sMesg);


     a= Convert.ToInt32(sResponseData).Length;

    //Geting the Script out of the message.

    if (a > 10)
    {
        string stemp = sResponseData;
        string[] sSplit = stemp.Split(new Char[] { ';'});
        foreach (string s in sSplit)
        {

            if ((s.Trim() != "Tag") & (s.Trim() != "StopStart"))
                sScript = s;
        }
    }
  }
 }
 catch (Execption ex)
 {
 MessageBox.Show(ex.Message); //Shows Error 
 }
BizApps
  • 6,048
  • 9
  • 40
  • 62
0

Before checking length of sResponseData make sure you check whether sResponseData is null or not.

Pritam Karmakar
  • 2,773
  • 5
  • 30
  • 49