我收到一条错误消息,我不知道如何解决该问题。这是原来的代码我有:未设置为一个对象的一个实例未将对象引用设置为对象按钮按下事件的实例
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