[英]Deserialization NullReferenceException Object Reference With Classes Within Classes
My TypeA class: 我的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;
}
}
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); 我得到的错误发生在var d = reader.Deserialize(file); The exception says There is an error in XML document(3,2) which is
异常显示XML document(3,2)中存在错误,该错误是
<TypeA>
Its always the start of the first element even with 20+ elements. 即使有20多个元素,它始终是第一个元素的开始。
The inner exception says NullReferenceException Object Reference is not set to an instance of an object. 内部异常表明NullReferenceException对象引用未设置为对象的实例。
The TypeA is not getting correctly instantiated clearly. TypeA不能正确正确地实例化。 I have 2 theories as to why.
关于原因,我有2种理论。
I have other variables in TypeA that are private as well, but everything gets instantiated in the constructor. 我在TypeA中还有其他私有变量,但所有变量都在构造函数中实例化。 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. 对于TypeB,不能从TypeA内初始化TypeB。 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).
需要调用FindTypeB(id)函数,TypeB在非常特定的时刻在程序中的其他位置组成(这是在我反序列化您的想法之前发生的,所以我认为这不是问题)。 I figured that by putting that under the set of m_TypeBTargetID it would solve that because that gets deserialized.
我认为通过将其放在m_TypeBTargetID的集合下将可以解决该问题,因为它会反序列化。 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. 您对
m_TypeBTargetID
设置非常m_TypeBTargetID
。 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;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.