NullReferenceException was unhandled.

I was sending a pictureframe to a client in synchronous mode which meant, the program gets blocked until the client is found or transfer is complete. Consequently, I shifted to asynchronous mode. Now as far as i can see, there's nothing wrong with the client/server bit of the code but the part, where I am copying the picture frame from a clipboard throws this exception. It's utterly preposterous ! Apparently NOTHING gets copied from the clipboard and the IDataObject ends up having a null status. This never happened in the synchronous communication mode, hell, i didnt even touch the frame grabbing portion of the code. Anybody know whats wrong

Answer this question

NullReferenceException was unhandled.

  • Quilnux

    I am sorry I am unable to help you with the information you posted.

    All I can tell is you are trying to use/pass null reference object.

    Good luck!



  • pexxx

    See the problem is that the data which i'm copying from the clipboard, its directly coming from the webcam attached to my pc, and trust me, its THERE. I'm supposed to send it AFTER i have copied it from the clipboard and transferred it to a byte array. Now sending/receiving bit doesnt even kick in before this; and NOTHING has changed in the snippette of code where I grab frame from my webcam and copy data on to the clipboard.It was working perfectly before this. Somehow, changing from sync to async has made the IdataObject end up having a null value; ALWAYS..... It doesnt make any sense.
  • vua_rua

    Hi,

    Which part of your code was changed to run asynchronously The copy-from-clipboard-part, or only the send to the client


  • 12HH

    Most probably you check clipboard BEFORE data is copied.

    In async mode probably data is sent to clipboard a bit after you checked.

    Another issue is you do not check if you have appropriate data in clipboard before start working with it.

    clipboard should be used as last chance. Isn;t there another approach to pass data

    HTH



  • incendy

    Ok, I think theres something wrong with the socket code aswell. I replaced the whole 'getting from cam and copying to clipboard' thing and now i'm only trying to send a pictured from a file. Now i get this error: "Only one usage of each socket address (protocol/network address/port) is normally permitted" :(
  • Johansson Johan

    Only the send to the client part was changed, for example, In converting from sync to async, "tcplistener.AcceptTcpClient" changed to "tcplistener.BeginAcceptTcpClient"; so on and so forth. Point being, you can think of the copy data from clipboard into an IDataObject as being a completely different function which was never altered.

    Hmmm, one thing that has caught my attention though is this; when communicating asynchronously, we need a callback delegate(whatever that is). Now I've made the copy-from-clipboard-function my call back delegate from within which i call the EndAcceptTcpClient method. This however should have no bearing upon the IDataObject ... right I mean, whatever has to go wrong should go wrong with the socket coding right, not the rest of the stuff. Here's the code:

    public static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
    TcpListener listener = null;
    private IDataObject rawImage;
    Image finalImage;

    private void timer1_Tick(object sender, EventArgs e)

    {
    timer1.Enabled =
    false;
    listener =
    new TcpListener(localAdd, 13000);
    startServer(listener);
    timer1.Enabled =
    true;
    }

     

    private void startServer(TcpListener listener)

    {
    tcpClientConnected.Reset();
    listener.Start();
    listener.BeginAcceptTcpClient(
    new AsyncCallback(showImage), listener);
    tcpClientConnected.WaitOne();
    }

    private void showImage(IAsyncResult ar)

    {
    try

    {
    TcpListener listener = (TcpListener)ar.AsyncState;
    TcpClient client = listener.EndAcceptTcpClient(ar);
    SendMessage(hWnd, WM_CAP_GET_FRAME, 1, 0);
    SendMessage(hWnd, WM_CAP_EDIT_COPY, 1, 0);
    rawImage =
    Clipboard.GetDataObject();
    //EXCEPTION THROWN IN THE VERY NEXT LINE; rawImage NULL
    finalImage = (
    Bitmap)rawImage.GetDat(DataFormats.Bitmap);
    picCapture.Image = finalImage;
    MemoryStream ms = new MemoryStream();
    NetworkStream ns = client.GetStream();
    finalImage.Save(ms,
    ImageFormat.Jpeg);
    Byte[] photo = new Byte[ms.Length];
    photo = ms.ToArray();
    long input;
    input = photo.Length;
    Byte[] output = BitConverter.GetBytes(input);

    //Send the size of the image

    ns.Write(output, 0, output.Length);

    //Send the image

    ns.Write(photo, 0, photo.Length);

    ns.Flush();
    ms.Flush();
    tcpClientConnected.Set();
    timer1.Enabled =
    true;
    }

    catch

    {
    MessageBox.Show("An error occured while connecting to the device, make sure it's plugged in properly");
    }
    }


  • NullReferenceException was unhandled.