1

I've got an error that I cannot seem to track down when my page is trying to load a list of files from the server.

I pulled this code out of a TFS shelf-set, and it was running when it was saved.

I have tried breaking down the line where the error occurs (commented out in the code shown) and added extra checks just in case the data happen to have null entries.

Here is a snippet of it:

if (roadmap != null)
{
    List<Guid> taskList = lstRoadmapTaskMapping.Select(o => o.TaskGuid).ToList();
    var list = SessionManager.CurrentContext.TaskDecisionDetails.Where(x => x.Task != null && x.Task.TaskGuid != null && taskList.Contains(x.Task.TaskGuid));
    var lstTaskDecisionDetail = new List<TaskDecisionDetail>(list);
    //List<TaskDecisionDetail> lstTaskDecisionDetail = SessionManager.CurrentContext.TaskDecisionDetails.Where(o => taskList.Contains(o.Task.TaskGuid)).ToList();

    lstOfTasks = lstTaskDecisionDetail.Select(o => o.TaskToStart).ToList();
    lstOfTasks.AddRange(lstTaskDecisionDetail.Select(o => o.AssociatedTaskGuid ?? Guid.Empty).ToList());

    lstOfTasks.AddRange(SessionManager.Current.CurrentContext.TaskDecisionDetailRoutes.Where(o => taskList.Contains(o.TaskDecisionDetail.Task.TaskGuid)).Select(o => o.NATaskGuid).ToList());
    List<Guid> roadmapMappingGuid = lstRoadmapTaskMapping.Where(o => lstOfTasks.Contains(o.TaskGuid)).Select(o => o.RoadmapTaskMappingGuid).ToList();

    treeHL.JSProperties["cp_NoDeleteIDs"] = string.Join(",", roadmapMappingGuid);
}

The exception is thrown at the green highlighted line in the screenshot below:

code screen

The error message is this common one:

Object reference not set to an instance of an object.

The question below goes fully into that error message.

What does "Object reference not set to an instance of an object" mean?

That is what got me to break the single line (commented out at the moment) into 2 lines, hoping that would solve my error.

The StackTrace is pointing to the Entity framework:

at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)

error screen

What else could be null in the expression?

How can I go about debugging this error?

I am also curious about what it means whenever the debugger stops on a green breakpoint instead of a yellow breakpoint.

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37

2 Answers2

0

It turned out that the database had corrupt entries.

Loading the data to an Entity was causing validation errors with its constraints.

I restored the database from a recent version and the issues went away.

I started to just delete this question, but it might help someone down the road.

0

Had the same issue with Entity Framework for .NET Framework.

Had a class Customer with a nested Dealer class, something like:

class Customer {
int Id;
Dealer Dealer;
}

class Dealer {
int Id;
bool IsActive;
}

The isActive flag on the Database was implemented as 'bit' (1 or 0), but the constraint on that column was that it can also be NULL. During migration the isActive flag was left out from INSERTS so it took on the default value of NULL for some Dealer records.

When running _customerRepository.GetAll() EntityFramework could not map the null to boolean within the Dealer object and apparently the Dealer object was not created, which caused the issue.

Hope that helps anyone.

Srutu-tutu
  • 25
  • 6
  • *Hope that helps anyone.* -- No. It's a totally different case. And it's not clear why you got a null reference because what you describe should throw a different exception. – Gert Arnold May 23 '22 at 18:10
  • @gert-arnold, Maybe EF SHOULD throw a more specific exception, but it didn't, this is why I discuss this here. Even if my reasoning about how EF handles such cases is not correct, the error I got is EXACTLY the same. When I tried `dealerRepository.GetAll()` , then I got the specific error, like: "_The 'IsActive' property on 'DEALER' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.Boolean_'. , but when fetching it as a NESTED object this is error I got. – Srutu-tutu May 24 '22 at 05:52
  • As I said, not the same exception. – Gert Arnold May 24 '22 at 06:47