我收到一條錯誤消息,我不知道如何解決該問題。這是原來的代碼我有:未設置爲一個對象的一個實例未將對象引用設置爲對象按鈕按下事件的實例
private void SendMessage(Command cmd, EndPoint sendToEP)
{
try
{
//Create the message to send.
Data msgToSend = new Data();
//msgToSend.strName = txtName.Text; //Name of the user.
msgToSend.cmdCommand = cmd; //Message to send.
msgToSend.vocoder = vocoder; //Vocoder to be used.
byte[] message = msgToSend.ToByte();
//Send the message asynchronously.
clientSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, sendToEP, new AsyncCallback(OnSend), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "UniProject-SendMessage()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
該錯誤消息是(按鈕按下事件)
對象引用。
爲什麼我得到這個錯誤信息,我該如何解決它?
幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET一個NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」獲得一些提示。 – 2013-03-20 01:54:54
該代碼片段中可能導致'NullReferenceException'的唯一情況是'clientSocket'爲null。你需要花一些時間學習如何調試你的代碼。 – 2013-03-20 03:06:07