.NET Components for Mobility

NullReferenceException in attachment saving

Last post 05-20-2010 10:15 PM by PeterFoot. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 04-21-2010 5:12 AM

    NullReferenceException in attachment saving

    Hi Peter, Sometimes I'm getting this exception when calling Attachment.Save(). The attachment download state is FullItem and my code is as simple as:
                            if (dialog.ShowDialog() == DialogResult.OK)
                            {
                                    attachment.Save(dialog.FileName);
                            }
    
    Here is the stacktrace:
    en InTheHand.IO.StreamHelper.CopyTo(Stream s, Stream destination, Int32 bufferSize)
    en InTheHand.IO.StreamHelper.CopyTo(Stream s, Stream destination)
    en InTheHand.WindowsMobile.PocketOutlook.Attachment.Save(String path)
    
    Let me know if I can be of any help fixing this.
    Filed under: , ,
  • 04-21-2010 5:30 AM In reply to

    Re: NullReferenceException in attachment saving

    I'm using 4.1, btw. So this thread doesn't help: http://inthehand.com/forums/t/3462.aspx
  • 05-06-2010 1:12 AM In reply to

    Re: NullReferenceException in attachment saving

    Any chance that the issue is related to the target path - does the folder exist? Does the file already exist and if yes could it still be open by your application or another?

    Peter

  • 05-06-2010 1:34 AM In reply to

    Re: NullReferenceException in attachment saving

    Hi Peter,
    The path already exists, I'll double check if the file it's opened.
  • 05-06-2010 4:48 AM In reply to

    Re: NullReferenceException in attachment saving

    The problem looks similar to the problem with blank emails, described in http://inthehand.com/forums/p/3364/10257.aspx#10257 Maybe it is related.
    First time I open an email or attachment all is glory, but second time it fails.
    In the case of attachments, AttachmentData is the property where the problem occur: Cannot evaluate expression.
    This is my code. First time I launch the dialog and click OK, works fine. Next times, it won't.
    if (dialog.ShowDialog() == DialogResult.OK)
    {
                attachment.Save(finalName);
    }
    
  • 05-06-2010 5:48 AM In reply to

    Re: NullReferenceException in attachment saving

    Are you disposing the attachment after the first use or calling it in a using block? What about the parent EmailMessage is this still valid and in scope?

    Peter

  • 05-06-2010 6:12 AM In reply to

    Re: NullReferenceException in attachment saving

    Hi Peter,
    I don't think so, because when I made that mistake always the ObjectDisposedException was thrown, and other properties of the object don't show this behavior.
    Obviously, I have not a dispose call or using block that I am aware of (and it is improbable).
  • 05-06-2010 3:43 PM In reply to

    Re: NullReferenceException in attachment saving

    Same error is obtained on our side too. We really need to have a solution for this asap.

        at InTheHand.IO.StreamHelper.CopyTo(Stream s, Stream destination, Int32 bufferSize)
       at InTheHand.IO.StreamHelper.CopyTo(Stream s, Stream destination)

     

    Thanks.

  • 05-06-2010 11:51 PM In reply to

    Re: NullReferenceException in attachment saving

    I've been able to locate the underlying error - a COM exception is raised when trying to retrieve the AttachmentDataBin stream the second time around. The error description is "The process cannot access the file because it is being used by another process". I believe I have a solution and am performing more testing. I will post back to the forum when I have news of this fix and 4.2 release.

    Peter

  • 05-14-2010 12:57 AM In reply to

    Re: NullReferenceException in attachment saving

     Hi Peter,

     This problem seems to persist in the 4.2 release. Can you please confirm?

     Thank you!

  • 05-18-2010 6:20 PM In reply to

    Re: NullReferenceException in attachment saving

    We have not been able to duplicate this issue on 4.2. However there is a separate issue which is affecting MessageFolder for which we are preparing a patch.

    Peter

  • 05-19-2010 12:58 AM In reply to

    Re: NullReferenceException in attachment saving

    Indeed the "NullReferenceException" was not reproduced but another error is thrown now: "The process cannot access the file because it is being used by another process. "

       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at InTheHand.WindowsMobile.PocketOutlook.Attachment.get_AttachmentData()
       at InTheHand.WindowsMobile.PocketOutlook.Attachment.Save(String path)

     

    The code below throws this error:

    for (int i = 0; i < email.Attachments.Count; i++)
     {
                                if (email.Attachments[ i].Name.Equals(pattern))
                                {
                                 
                                    email.Attachments[ i].Save(tempFile);

                                    break;

                                } 

    }

     

    Thanks for your help!

     

  • 05-20-2010 10:15 PM In reply to

    Re: NullReferenceException in attachment saving

    The attachment can only be opened once and your code is causing two accesses. Also call Dispose to release resources (otherwise the attachment may remain open until the next garbage collection). Try this instead:-

    for (int i = 0; i < email.Attachments.Count; i++)

    {

      Attachment attach = email.Attachments[ i];
      if (attach.Name.Equals(pattern))
      {

        attach.Save(tempFile);

        attach.Dispose();

        break;

      } 

      attach.Dispose();

    }

    Alternatively you can achieve the same with a for each loop:-

    foreach(Attachment attach in message.Attachments)

    {

      if(attach.Name.Equals(pattern))

      {

        attach.Save(tempFile);

        break;

      }

    }

Page 1 of 1 (13 items)
Copyright © 2001-2012 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy. OrcsWeb's Windows Cloud Server Hosting