2012-02-12 93 views
0

可能重複:
NullReferenceException on instanciated object?
What is a NullReferenceException in .NET?異常信息:System.NullReferenceException

我已經在C#中內置了TCP客戶端,當客戶端無法連接即時得到以下。 我不斷收到一個NullReference異常與下面的代碼,我不知道如何捕捉它或停止它..任何幫助將不勝感激。

其在 「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; 
      } 
     } 
    } 

}

和這裏發生的一切是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."); 
     } 

    } 
+2

在代碼的哪一行會得到異常?閱讀整個異常消息。 – 2012-02-12 23:41:37

+1

該行唯一可以爲空的是'sResponseData'。所以我猜測第二個函數永遠不會被調用,但不知道沒有獲得更多的信息。啓動調試器,並驗證sResponseData實際上是否爲null,您收到錯誤的位置,您的第二個函數是否被調用,如果不是,則無論哪個函數調用它來查看爲什麼會被跳過,等等。 – millimoose 2012-02-12 23:42:49

回答

0

您可以檢查是否響應數據具有價值與否:

類似於:

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 
} 
0

在檢查sResponseData的長度之前,請確保檢查sResponseData是否爲空。