简体   繁体   中英

Deserialization NullReferenceException Object Reference With Classes Within Classes

My TypeA class:

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

My Main:

   public ObservableCollection<TypeA> A_Collection { get; set; }
    public void DeSerializeTypeACollection()
    {
        try
        {
            XmlSerializer reader = new XmlSerializer(typeof(ObservableCollection<TypeA>));
            System.IO.TextReader file = new System.IO.StreamReader(
                @"c:\temp\myXML.xml");
            var d = reader.Deserialize(file);
            A_Collection = (ObservableCollection<TypeA>)d;
            file.Close();
        }
        catch { }
    }

The result of serializing with one element in the collection and thus want I want to serialize:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTypeA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <TypeA>
   <m_Title>test2</m_Title>
   <m_TypeBTargetID>32770</m_TypeBTargetID>
  </TypeA>
</ArrayOfTypeA>

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

 <TypeA>

Its always the start of the first element even with 20+ elements.

The inner exception says NullReferenceException Object Reference is not set to an instance of an object.

The TypeA is not getting correctly instantiated clearly. I have 2 theories as to why.

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.

Or:

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?

EDIT:

Stack Trace-

+       $exception  {System.InvalidOperationException: There is an error in XML document (3, 4). ---> 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}

Your setter for m_TypeBTargetID is quite strage. Seems like you forgot your private variable... Try this:

[Serializable]
public class TypeA
{
  public string m_Title { get; set; }

  private TypeB _targetB
  [XmlIgnore]
  public TypeB m_Target 
  {
     get //Do you need that setter?
     {
        if (_targetB == null)
            _targetB = FindTypeB(m_TypeBTargetID);

        return _targetB;
     }
  }

  public Int32 m_TypeBTargetID {get; set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM