0

We migrated our codes from .Net Framework 2.0 to .Net Framework 4.0, but when we tested it we encountered an error which is about "Object reference not set to an instance object".

We tried to look for possible solution for this and saw this article about obsolete types and members of .Net Framework 4.0 https://msdn.microsoft.com/en-us/library/ee471421(v=vs.100).aspx. One of the members that was listed there is being used in one of our class namely "System.Xml.Serialization.XmlSerializer" but still even though that we keep building our solution no errors are being found. Is this really the behavior of an obsolete member/type? Or do you think that the error regarding "Object reference not set to an instance object" is related to these obsolete member/type?

        #region Variables Setup
        //Declare Variables
        string orderUID = string.Empty;
        string input = string.Empty;

        APSArchitecture.Tools.APSLogData logData = null;
        XmlDocument bodyDoc = null;
        IBaseMessagePart bodyPart = null;
        const string APSHEADER_PARTNAME = "APSHeader";
        APSArchitecture.Tools.Serialization.APSHeader apsHeader = null;         
        APSArchitecture.Pipelines.NonSeekMemoryStream APSHdrStream = null;
        APSArchitecture.Pipelines.NonSeekMemoryStream copyOfBodyStream = null;
        **XmlSerializer serializer** = null;
        Microsoft.BizTalk.Message.Interop.IBaseMessagePart part = null;
        string partname = string.Empty;
        XmlNamespaceManager nsmgr = null;
        Stream originalBodyStream = null;
        #endregion

        #region Init Variables
        //Initial Variables
        try 
        {
            logData = new APSArchitecture.Tools.APSLogData();
            logData.SrcSystem = "AssignOrderUID.Execute";
            bodyDoc = new XmlDocument();
            bodyPart = pInMsg.BodyPart;
            originalBodyStream = bodyPart.Data;
            copyOfBodyStream = new NonSeekMemoryStream();
            //
            // Copy the original message to the copy stream for reading of the xml.
            //
            const int ONE_K = 1024;
            byte[] buffer = new byte[ONE_K];
            int readSize = 0;
            while((readSize = originalBodyStream.Read(buffer,0,ONE_K)) != 0)
            {
                copyOfBodyStream.Write(buffer,0,readSize);
            }
            copyOfBodyStream.Seek(0,SeekOrigin.Begin);
        }
        catch(System.Exception ex)
        {
            string error = ex.Message;
            if(ex.InnerException != null)
                error = error + " Inner Exception Details: " + ex.InnerException.Message;
            throw new ApplicationException("Error initilalizing AssignOrderUID Execute method. Exception Details: " + error, ex);
        }
  • `XmlSerializer` itself isn't obsolete - just a few methods have been marked as obsolete. Unfortunately it's hard to know what's wrong without knowing anything about where the exception occurred. – Jon Skeet Jul 27 '16 at 16:21
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Charles Mager Jul 27 '16 at 16:21
  • 3
    Use proper logging and log the *entire* exception, not just the message. The exception includes the location and the call stack. It's extremely easy to do, just use `Exception.ToString()`. That will show you where the problem is. – Panagiotis Kanavos Jul 27 '16 at 16:34
  • I hope you don't write such code... Way to much variables in your function! Also, where are your `using` clauses? – Phil1970 Jul 27 '16 at 17:06
  • If you run it with the debugger Attached, you will see exactly where it breaks. – Johns-305 Jul 29 '16 at 08:21

0 Answers0