簡體   English   中英

類中具有類的反序列化NullReferenceException對象引用

[英]Deserialization NullReferenceException Object Reference With Classes Within Classes

我的TypeA課:

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

我的主:

   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 { }
    }

使用集合中的一個元素進行序列化的結果,因此希望我要序列化:

<?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>

我得到的錯誤發生在var d = reader.Deserialize(file); 異常顯示XML document(3,2)中存在錯誤,該錯誤是

 <TypeA>

即使有20多個元素,它始終是第一個元素的開始。

內部異常表明NullReferenceException對象引用未設置為對象的實例。

TypeA不能正確正確地實例化。 關於原因,我有2種理論。

我在TypeA中還有其他私有變量,但所有變量都在構造函數中實例化。 我在某處讀到,反序列化不會調用構造函數,因此需要初始化的任何內容都需要在其他地方初始化。 雖然我不確定。

要么:

對於TypeB,不能從TypeA內初始化TypeB。 需要調用FindTypeB(id)函數,TypeB在非常特定的時刻在程序中的其他位置組成(這是在我反序列化您的想法之前發生的,所以我認為這不是問題)。 我認為通過將其放在m_TypeBTargetID的集合下將可以解決該問題,因為它會反序列化。 也許那是我的問題,而不是私有變量?

編輯:

堆棧跟蹤-

+       $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}

您對m_TypeBTargetID設置非常m_TypeBTargetID 好像您忘記了您的私有變量...請嘗試以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM