title: Deserialization NullReferenceException Object Reference With Classes Within Classes
body: <p>My TypeA class:</p>
  
  <pre><code>   [Serializable]
     public class TypeA
     {
       public string m_Title { get; set; }
  
       [XmlIgnore]
       public TypeB m_Target {get; set;}
  
       public Int32 m_TypeBTargetID
       {
           get { return m_Target.m_ID; }
           set
           {  //the ID is passed to a function that returns
              //a specific TypeB that is in a list in the main thread
               m_Target = FindTypeB(value);
               m_TypeBTargetID= value;
           }
       }
  </code></pre>
  
  <p>My Main:</p>
  
  <pre><code>   public ObservableCollection&lt;TypeA&gt; A_Collection { get; set; }
      public void DeSerializeTypeACollection()
      {
          try
          {
              XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection&lt;TypeA&gt;));
              System.IO.TextReader file = new System.IO.StreamReader(
                  @"c:\temp\myXML.xml");
              var d = reader.Deserialize(file);
              A_Collection = (ObservableCollection&lt;TypeA&gt;)d;
              file.Close();
          }
          catch { }
      }
  </code></pre>
  
  <p>The result of serializing with one element in the collection and thus want I want to serialize:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ArrayOfTypeA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;TypeA&gt;
     &lt;m_Title&gt;test2&lt;/m_Title&gt;
     &lt;m_TypeBTargetID&gt;32770&lt;/m_TypeBTargetID&gt;
    &lt;/TypeA&gt;
  &lt;/ArrayOfTypeA&gt;
  </code></pre>
  
  <p>The error I'm getting occurs at var d = reader.Deserialize(file);
  The exception says There is an error in XML document(3,2) which is </p>
  
  <pre><code> &lt;TypeA&gt;
  </code></pre>
  
  <p>Its always the start of the first element even with 20+ elements.</p>
  
  <p>The inner exception says NullReferenceException Object Reference is not set to an instance of an object.</p>
  
  <p>The TypeA is not getting correctly instantiated clearly. I have 2 theories as to why.</p>
  
  <p>I have other variables in TypeA that are private as well, but everything gets instantiated in the constructor. I read somewhere that deserializing does not call the constructor so anything that needs to be initialized needs to be initialized elsewhere. Where though I'm not certain.</p>
  
  <p>Or:</p>
  
  <p>As for TypeB, TypeB cannot be initialized from within TypeA. It NEEDS that FindTypeB(id) function to be called, TypeB is made up elsewhere in the program at a very specific moment (which happens before I deserialize mind you so I do not believe that is the issue). I figured that by putting that under the set of m_TypeBTargetID it would solve that because that gets deserialized. Perhaps thats my issue rather than the private variables?</p>
  
  <p>EDIT:</p>
  
  <p>Stack Trace-</p>
  
  <pre><code>+       $exception  {System.InvalidOperationException: There is an error in XML document (3, 4). ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
     at Application.TypeA.Shutdown() in TypeA.cs:line 150
     at Application.TypeA.set_m_Enabled(Boolean value) in TypeA.cs:line 53
     at Application.TypeA..ctor() in TypeA.cs:line 68
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderObservableCollection1.Read3_TypeA(Boolean isNullable, Boolean checkType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderObservableCollection1.Read4_ArrayOfTypeA()
     --- End of inner exception stack trace ---
     at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
     at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
     at Application.MainWindow.DeSerializeAlerts() in MainWindow.xaml.cs:line 393}    System.Exception {System.InvalidOperationException}
  </code></pre>