-1

I'm getting an error message and I don't know how to fix it. This is the original code I have:

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);
    }
}

The error message is (button press event)

Object reference not set to an instance of an object.

Why am I getting this error message and how can i fix it?

Daniel A.A. Pelsmaeker
  • 40,431
  • 19
  • 96
  • 149
Roshi End
  • 41
  • 1
  • 7
  • 4
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Mar 20 '13 at 01:54
  • 1
    The only thing in that code snippet that could cause the `NullReferenceException` is `clientSocket` being null. You need to spend some time learning how to debug your code. – Jeremy Thompson Mar 20 '13 at 03:06

1 Answers1

4

Every time you get such an error (a NullReferenceException), there is something in your code that is set to null. You have to look at your code and determine:

  1. On which line (in which method) does the error occur?
  2. What variables on that line are reference types or nullable value types?
    Normal value types (e.g. struct, or integers, floats, doubles) cannot be null.
  3. Of those variables, which could possibly be null?
  4. Where are those variables possibly set to null?
    For example, a method argument, a value returned from a method, or the result of the as operator can result in a variable being null.

If none of those is the case, you might have (although unlikely) a method that is throwing this exception. The .NET base class methods generally don't throw such an exception, and if your code does throw it, your stack trace should bring you to the deepest method and line that does that.

Daniel A.A. Pelsmaeker
  • 40,431
  • 19
  • 96
  • 149
  • With the exception of nullable value types of course, which are still value types – David L Mar 20 '13 at 02:03
  • 1
    @DavidL Nice catch. Nullable types are actually kind of _in-between_ reference types and value types, and I forgot about them. For example, you can constrain a generic type to `class` for reference types, or `struct` for _non-nullable_ value types. Nullable value types are special. – Daniel A.A. Pelsmaeker Mar 20 '13 at 02:17
  • I agree with Virtlink that values types are quite special. On *no other value type* can you get a `NullReferenceException` when using dot notation against the boxed representation of that value type. That's pretty special to me. – Kirk Woll Mar 20 '13 at 02:44
  • @Virtlink, what's an example where you can get the NRE with any other boxed value type? – Kirk Woll Mar 20 '13 at 02:54
  • @KirkWoll Ah I see what you mean, I misunderstood you. There is _no_ non-nullable value type that, when boxed, will returned in a `NullReferenceException`. – Daniel A.A. Pelsmaeker Mar 20 '13 at 03:16