Hello,
We are migrating our application to DataObjects 4.0.5 and I've a problem with abstract classes: I'm getting NullReference exceptions when trying to use an object with it's abstract base class.
Am I doing something wrong?
Persistent classes :
[HierarchyRoot]
public abstract class Trigger : Entity
{
[Field, Key]
public long Id { get; private set; }
public abstract string ToText();
}
public class SimpleTrigger : Trigger
{
[Field]
public TimeSpan Delay { get; set; }
public override string ToText()
{
return Delay.ToString();
}
}
Code (without domain build)
using (Session.Open(domain))
using(TransactionScope transactionScope = Transaction.Open())
{
new SimpleTrigger { Delay = TimeSpan.FromSeconds(1.0) };
transactionScope.Complete();
}
using (Session.Open(domain))
using (TransactionScope transactionScope = Transaction.Open())
{
SimpleTrigger myTrigger = Query<SimpleTrigger>.All.First<SimpleTrigger>();
Console.WriteLine(myTrigger.ToText()); // working as expected
transactionScope.Complete();
}
using (Session.Open(domain))
using (TransactionScope transactionScope = Transaction.Open())
{
Trigger myTrigger = Query<Trigger>.All.First<Trigger>();
Console.WriteLine(myTrigger.ToText()); // NullReferenceException
transactionScope.Complete();
}
Exception below :
System.NullReferenceException was unhandled
Message="La référence d'objet n'est pas définie à une instance d'un objet."
Source="Xtensive.Core"
StackTrace:
à Xtensive.Core.Tuples.Tuple.GetValueOrDefault[T](Int32 fieldIndex)
à Xtensive.Storage.Internals.DefaultFieldAccessor`1.GetValue(Persistent obj, FieldInfo field)
à Xtensive.Storage.Persistent.GetFieldValue[T](FieldInfo field)
à Xtensive.Storage.Persistent.GetFieldValue[T](String fieldName)
à TestDO4.SimpleTrigger.get_Delay()
à TestDO4.Program.Main(String[] args) dans Visual Studio 2008\Projects\TestDO4\TestDO4\Program.cs:ligne 35
à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
à System.Threading.ThreadHelper.ThreadStart()
InnerException:
Regards,
Updated at 11.09.2009 13:37:01
After some more investigation, this problem occurs also with virtual methods.
Class not abstract, but with an overridden virtual method.
[HierarchyRoot]
public class Trigger : Entity
{
[Field, Key]
public long Id { get; private set; }
public virtual string ToText()
{
return null;
}
}
public class SimpleTrigger : Trigger
{
[Field]
public TimeSpan Delay { get; set; }
public override string ToText()
{
return Delay.ToString();
}
}
When calling Trigger.ToText() I get the same null reference exception on Delay.
Regards,
Updated at 11.09.2009 14:27:38
Thanks for your really fast answer: I'll use your work around, it is much better than mine.
Regards,
This thread was imported from our support forum. The original discussion may contain more detailed answer.
asked
Sep 11 '09 at 13:28
olorin
358●87●87●92
The problem looks really strange. We'll try to isolate it during this weekend.
Bug report: http://code.google.com/p/dataobjectsdot ... ail?id=395
Already fixed in 12502 revision.
See nightly build or wait for 4.0.6.
P.S. In case you can't wait or use nightly build there is a workaround: you should mark these fields with [Field(LazyLoad = true)] attribute.