Print Page | Close Window

Strange null reference

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2714
Printed Date: 28-Jun-2026 at 8:37am


Topic: Strange null reference
Posted By: midnit
Subject: Strange null reference
Date Posted: 25-May-2011 at 5:12am
I have a random error popping up in my model.
 

/// <summary>Gets or sets the SecAppUser. </summary>

[Bindable(false)]

[Display(Name="SecAppUser", AutoGenerateField=false)]

[DataMember]

[IbEm.RelationProperty("SecAppUser_SecUserPrivilege", IbEm.QueryDirection.ToRole2)]

public SecAppUser SecAppUser {

get { return PropertyMetadata.SecAppUser.GetValue(this); } <--------null reference here sometimes

set { PropertyMetadata.SecAppUser.SetValue(this, value); }

}

Seemingly at random the getter throws a null reference. I do not make any direct reference to this property so I am unsure why its "gotten" in the first place. When the error is thrown if I reference it through the immediate window it returns a value fine. The error is as follows:

Object reference not set to an instance of an object. at IdeaBlade.EntityModel.NavigationScalarEntityProperty`2.GetValue(TInstance instance)
   at Png.GcsAg.Model.Common.SecUserPrivilege.get_SecAppUser()
   at WriteSecUserPrivilegeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteRelatedEntityListOfSecUserPrivilege_SWPOidsLToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteSecAppUserToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteSecUserPrivilegeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteArrayOfSecUserPrivilegeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteGcsPrincipalToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteSessionBundleToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, Object[] parameters)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BodyWriter.WriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
   at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
   at System.ServiceModel.Channels.BinaryMessageEncoderFactory.BinaryMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   at IdeaBlade.Core.Wcf.Extensions.GZipMessageEncoderFactory.GZipMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   at System.ServiceModel.Channels.MessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager)
   at System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message)
   at System.ServiceModel.Channels.HttpOutput.SendAsyncResult.SendWithoutChannelBindingToken()
   at System.ServiceModel.Channels.HttpOutput.SendAsyncResult..ctor(HttpOutput httpOutput, Boolean suppressEntityBody, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.HttpOutput.BeginSend(TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.SendWebRequest()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.BeginSendRequest(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.StartSend(Boolean completedSynchronously)
 
Any ideas on where to look? If I ignore the error things appear to continue as expected.



Replies:
Posted By: DenisK
Date Posted: 25-May-2011 at 4:23pm
Hi midnit;

You're the 3rd customer who has experienced this specific random errors. Unfortunately, we're still unable to repro even after using clues from the previous 2 reported issues. Fortunately, there was a workaround (or two).

The first issue can be read here.  http://www.ideablade.com/forum/forum_posts.asp?TID=1885 - http://www.ideablade.com/forum/forum_posts.asp?TID=1885 . In this particular case, the workaround was to refactor out the IPrincipal, IIdentity interface to using DevForce UserBase class. For example,

public partial class User : IbEm.Entity, IPrincipal, IIdentity

to

public partial class User : IbEm.Entity, IdeaBlade.EntityModel.UserBase

The second issue was sent through the support web form so it's not available to be shared. But in that particular case, the customer's workaround was to remove his custom LoginManager entirely.

So with that said, I'm hoping your case can provide more clues so we can repro and pin this down. The previous 2 cases (and your case?), seem to point that the problem is somewhere within the security model. Would you be able to provide the implementation of your SecAppUser class and the class this property is defined on? And so far, are you seeing the exception on this property only?


Posted By: midnit
Date Posted: 26-May-2011 at 4:04am
I can provide these items. In our implementation it is strictly a standard model, nothing security specific though these items are used in the LoginManager to perform login and for passing the privileges. In fact...I think I remember when this started cropping up, I will revisist the code changes and see if I can make it stop.
 
I will follow up shortly.


Posted By: midnit
Date Posted: 26-May-2011 at 4:37am
Ok, it appears that I can stop it from happening. Being random I can't say for sure but I spent 10 minutes in the app with no issue (usually takes about 1-2 minutes to occur).
 
I have a temp principal object I am working with and for testing I had added an IEnumerable of SecUserPrivilege. When a user (SecAppUser) in the LoginManager and then I was setting GcsPrincipal.Privileges = user.SecUserPrivileges. If I build this collection (using say a privilege class) rather then using the navigation I do not get the null reference. Does that make sense?
 
I have attached (or am going to try to) a zip with the loginmanager and the model. There are no partials on the model. The loginmanager as included does not erro, but I commented the line that would make it throw the randomness.
 
http://www.ideablade.com/forum/uploads/531/nullreference.zip - uploads/531/nullreference.zip


Posted By: DenisK
Date Posted: 26-May-2011 at 11:39am
Thanks for the update and files midnit. I'll get back to you once I have more info/questions.


Posted By: DenisK
Date Posted: 31-May-2011 at 12:29pm
Hi midnit;

Would you be able to upload your debug log during a failed run? I have created test cases with a similar LoginManager and a Principal object structure but I still would like to know what kind of operation that we're doing on the client that will trigger the exception. Thanks.


Posted By: DenisK
Date Posted: 16-Jun-2011 at 12:02pm
Hi midnit;

Just want to follow up on this to see if you'll be able to provide the information above. It will greatly help me track down this curious bug. Thanks!


Posted By: midnit
Date Posted: 16-Jun-2011 at 12:15pm
Ugh, I feel really bad about this. I tracked down the issue and had a sample to reproduce it.
 
My computer failed since then and since I had fixed our app when I was done rebuilding my computer I forgot all about this problem. That sample was on the old PC and I can't remember the specifics. I am going over my source control history to see if I can find the source that had the problem and maybe it will refresh my memory.


Posted By: midnit
Date Posted: 16-Jun-2011 at 12:31pm
I'm not getting anything.
 
I do remember that if I returned a collection of entities that had a navigation property in an IPrincipal (in this case from my login manager) it would lead to the error. I believe if I removed the navigation property I did not have the problem or if I created a custom object (my final soluction) to fill the collection there was no problem. There was no specific item that triggered the exception that I could track down. It could have been related to checking IsInRole (which did not linq on that collection) but I had nothing that directly accessed the navigation property that was throwing the null exception. Also at the time of error if you inspect the value that just threw the exception...it was NOT null. To get the error I would just refresh the page over and over until it eventually threw the error. On each row it would check IsInRole which is in that code I included above. I was calling the
 
public bool IsInRole(string componentCode)
{
//TODO: revisit
return (from item in Components where string.Concat(item.SecAppCatCd + item.SecAppComp).ToUpper() == componentCode.ToUpper() select item).FirstOrDefault().IsNotNull();
}
 
Sorry I dropped this...


Posted By: DenisK
Date Posted: 21-Jun-2011 at 7:17pm
Hi midnit;

Thank you. I really appreciate your time on this. This is certainly more info than before. 

You said that to get the error, you just refresh the page over and over. Did you have any initial query to populate the grid on a page? If so, what kind of query?



Print Page | Close Window