changedirector.update(TrackedObject item) throws null reference exception - c#

When looking on the internet for this error i get one hit that is close to the error i am getting. It can be found here: https://social.msdn.microsoft.com/Forums/en-US/8ab51524-e8d5-4eb3-acba-4ff024142e57/nullreferenceexception-in-getupdatecommand-during-submitchanges-call?forum=linqtosql
The solution provided by this person was to rewrite the code, however this is not really an option in this case.
So what happens is that I open a new datacontext connection to our database using linq to sql. Then with or without updating a record a submitchanges is called and the following error is thrown:
Exception: Object reference not set to an instance of an object.
(no InnerException)
StackTrace: at System.Data.Linq.ChangeDirector.StandardChangeDirector.Update(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at Brainsch.DLinq.BrainschDataContext.SubmitChanges(ConflictMode failureMode)
This exception keeps recurring until the application is restarted. After the application is restarted it does not occur any more.
I found the code part where this error happens(https://referencesource.microsoft.com/#System.Data.Linq/ChangeDirector.cs,f4d313ee41bcda98) and looking at that code gives me many options where the null reference exception occurs.
Can anyone share some light on why this error occurs and how to circumvent it.

Related

How to fix an ArgumentNullException (or NullReferenceException?) occurring in Enumerable.ToDictionary? (C# ASP.NET MVC)

I have an exception that is occurring, but I don't quite understand how it can be occurring. I would appreciate any tips on how to fix it, or advice on how I can better understand why the error is occurring.
Background Info
The error was logged to our error database, and I unfortunately haven't been able to reproduce the error myself.
This error occurred in a dashboard application that runs 24/7, and it attempts to refresh the data every minute. As such, this error has occurred multiple times in the last few weeks. While it's not exactly an urgent issue since the dashboard re-attempts to refresh data soon after, it bothers me that I haven't been able to fix it.
The error
This is the exception message that was logged: Object reference not set to an instance of an object. Looking it up, it seems like this is caused by a NullReferenceException. However, the Exception is occurring in Enumerable.ToDictionary(), which, according to the documentation, only throws an ArgumentNullException. Thus, I'm confused on which error is occurring, how, and why it is occurring.
Here is the relevant part of the stack trace.
at PackerVM.<>c.<GetData>b__32_3(OrderDetail orderDetail) in PackerVM.cs:line 72
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
at PackerVM.GetData() in PackerVM.cs:line 65
The code
Here is the code where the error is occurring.
_orderDetails = miiDb.OrderDetails // OrderDetails is a DbSet<OrderDetail>
.Where(orderDetail => orderDetail.Department == "MOLDING" && orderDetail.Location != null)
.GroupBy(orderDetail => orderDetail.Location)
.Select(orderDetails => orderDetails.OrderByDescending(w => w.ReleaseState)
.ThenBy(orderDetail => orderDetail.Sequence)
.ThenBy(orderDetail => orderDetail.PlannedStart)
.FirstOrDefault())
.ToDictionary(orderDetail => orderDetail.Location, orderDetail => new PackerPressData()
{
Location = orderDetail.Location,
MachineStatus = orderDetail.MachineStatus,
ReleaseState = orderDetail.ReleaseState,
Tool = orderDetail.Tool,
PartNumber = orderDetail.PartNumber
});
Here is some additional details on the line numbers in the stack trace:
line 72 refers to this section of code: .ToDictionary(orderDetail => orderDetail.Location, orderDetail => new PackerPressData()
line 65 refers to this section of code: _orderDetails = miiDb.OrderDetails
My progress
The error is occurring in Enumerable.ToDictionary, so I started by looking up the Microsoft docs for Enumerable.ToDictionary, which is how I determined that the error is an ArgumentNullException. As mentioned before though, the message seems to correspond to a NullReferenceException, leading to my confusion.
This is basically where I'm stuck though as I haven't been able to reproduce the issue. As I mentioned, the data updates frequently, so my testing likely just hasn't ran for long enough to encounter it again.
I've tried manually adding checks when testing locally to see if any of the OrderDetails produced by the .Select() statement preceding the .ToDictionary() statement are null, but none were null.
The only thing I've been able to think of is to add a Where clause: .Where(orderDetail => orderDetail != null) between the .Select() and .ToDictionary() statements, but I'm not convinced that this would fix the root cause of the issue since I don't completely understand how an OrderDetail at that point can be null in the first place.
TLDR
Enumerable.ToDictionary() seems to be producing an ArgumentNullException, but the error message saved to our Error database corresponds to a NullReferenceException. Additionally, I don't understand how the objects used to produce the Dictionary object can be null at that point in the code.

Invalid object name 'dbo.EdmMetadata'

I keep getting this error on initial load of my application. I have searched high and low and the only thing I found to do was the code below in my context.
Database.SetInitializer<Context>(null);
I have this set in the OnModelCreating method but it is still throwing the error below:
System.Data.SqlClient.SqlException: Invalid object name
'dbo.EdmMetadata'.
System.Data.Entity.Core.EntityCommandExecutionException: An error
occurred while executing the command definition. See the inner
exception for details.
You should add this code in constructor of your Context class.

'System.Reflection.TargetInvocationException' when calling Activator.CreateInstance()

I am receiving this error:
A first chance exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll
and also:
Exception has been thrown by the target of an invocation.
when running this method, and the error is occurring on the CreateInstance(Type.GetType(instantiationString), parameters) call:
public static Report instantiateReport(ReportInfo reportInfo)
{
string instantiationString;
Report reportToReturn;
ReportInfo[] parameters = new ReportInfo[1];
parameters[0] = reportInfo;
instantiationString = reportInfo.Report.InstantiationPath;
reportToReturn = (Report)Activator.CreateInstance(Type.GetType(instantiationString), parameters);
return reportToReturn;
}
ReportInfo is just a custom class that contains variables that help me build the report that I am trying to instantiate and when I investigate it, everything is completely fine. The instantiationString just helps me understand which report is trying to be created at the call and that way I can use the Type.GetType() method. I really have no idea what kind of error this is or what could be going wrong. I researched it a bit but didn't receive any helpful answer so any help is appreciated.
EDIT After looking at the inner exception I am getting Object reference not set to an instance of an object. message. This is strange because neither the ReportInfo or the GetType() methods have a null reference. Say one of the variables within ReportInfo was null, could that cause an issue? It shouldn't right?
FIXED After delving into the stack trace of the inner exception I found that there was some conflicting code causing issues elsewhere. Problem solved.

Emgu.CV , CvBlob.BoundingBox throw System.AccessViolationException

Im using Emgu.CV (OpenCV), to find delta in image, but sometimes I get Access violation exception that cause my app to crash.
After digging in the debug I find that (blobs.Values):
List<CvBlob> listOfBlobs = blobs.Values.ToList();
return 1733 items, and when I do the following:
But when run through the list I get EXCEPTION:
if (resultedRectangles[j].Contains(listOfBlobs[i].BoundingBox))
I check and find the exception occurred at: i = 418 :
+BoundingBox '(new System.Collections.Generic.Mscorlib_CollectionDebugView(listOfBlobs)).Items[418].BoundingBox'
threw an exception of type
'System.AccessViolationException' System.Drawing.Rectangle
{System.AccessViolationException}
As I see the last valid value in the list is in 417.
I have 2 questions:
1. Why blobs.Values.ToList(); return such corrupt data?
2. How I can check the value before access it to prevent System.AccessViolationException ?
Are you having multiple threads in your Process? If there are multiple threads trying to initialize the List, then the list may get corrupted.
This exception is more specific to Memory related issues and you will be in a hard time to debug this, unless the all the code is in your control. The following link may help.
http://msdn.microsoft.com/en-us/library/system.accessviolationexception(v=vs.110).aspx
Me too got trapped in the same error.

Problem with Environment.StackTrace

On a thread that is processing new data in the system, if the data is invalid I write a message in the event log, containing the Environment.StackTrace information.
Writing in the event log throws an exception with no text message
Message:
CallStack - at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at <my method that writes in the event log>
Any ideas why this happens?
EDIT: I am interested in what can cause the Environment.StackTrace to throw exception in general, so that i can understand what is happening in my case
#anchandra I don't know if you have figured this out since, as this is now 4 years old.
However I would like to add as I just stumbled upon this myself.
So, why does Environment.StackTrace throws an exception?
First the most uninteresting answer:
As you can see in the MSDN reference to the property, it can throw an ArgumentOutOfRangeException if "The requested stack trace information is out of range.", note also that the executing context must have System.Security.Permissions.EnvironmentPermission.
Now what stumped me for a second:
It does NOT throw an exception! Yes, that is what was happening to me, it actually returned me a Stack Trace that started listing the call to Environment.StackTrace like "at System.Environment.get_StackTrace()" then it showed all the other calling methods.
Because I was logging this into an audit log, if you look at it you assume there is an exception occurring at the last frame of the stack, but that's not true, I was just happening to request a Stack Trace at that point and stick it in my error log, very dumb once I realized this.
You need to capture the stacktrace before moving onto a separate thread.
Stacktrace will only show you the frames up to the root of the thread.

Categories

Resources