-2

I'm trying to iterate over all the results from a linq statement. I understand that the linq statement is not executed until the foreach loop starts. The code steps through the foreach loop once with a result but when it tries to loop through a second time it throws the error "Object references not set to an instance of an object" on the object "SelectectedEntity".

List<FeedEntity> EntityList = feed.entity.ToList();

IEnumerable<FeedEntity> SelectectedEntity = 
     from entity in EntityList  
     where entity.trip_update.trip.trip_id == "9571620-BCC 17_18-BCC_FUL-M-Tu-W-Th-01" 
     select entity;

foreach (FeedEntity e in SelectectedEntity)
{
    string s = e.trip_update.trip.route_id.ToString();
}

I'm expecting a few dozen elements in the SelectedEntity collection. What am I missing or have I misunderstood how linq operates.

The question is not what is a null reference, as I know what that is ..but why was I getting a null reference at this point in the code.

It had to do with the data and how linq interprets it. I'll post the correct code below as the answer.

ErickTreetops
  • 3,189
  • 4
  • 27
  • 37
  • It would certainly help if you learned the basics of c# naming: public properties are named with PascalCase and without `_` (underscores) and variables are named with camelCase. Your problem is that `e.trip_update.trip.route_id` is `null`, so `ToString` fails – Camilo Terevinto Feb 01 '18 at 00:58
  • You are getting an error because in the loop trip_update or trip or route_id is null and trying to dereference null generates an exception. You need to be able to handle missing values. – Ray Fischer Feb 01 '18 at 01:11
  • Yes , you are right the return list did contain objects that did not contain a trip_route or trip object. However the failure occurs during the iteration foreach loop not the string cast. In regards to the naming convention if you are refering to .trip_update.trip.route_id These are the object names as defined in the protobuf-net Nuget assembly use to deserialize real time transport data into C# objects. You'll need to talk to Google as it's their approved cross language standard which is why it doesn't follow c# naming standards. – ErickTreetops Feb 01 '18 at 02:02
  • I'd like to provide the answer on how the linq should be written but as it was marked duplicate that option is missing. Knowing what a null reference is won't stop you making this linq mistake.. – ErickTreetops Feb 01 '18 at 02:10

1 Answers1

-1

"Object references not set to an instance of an object" means that you try to access an object, while this object equals null:

MyClass myObject = null;
string name = myObject.Name;

So you have an entity list, and some horrible string, and you want only those elements form your entity list where entity.trip_update.trip.trip_id equals horrible string.

Are your sure that entity not is null, and entity.trip_update not is null and ... not is null?

You should check this in your query

const string horribleString = "9571620-BCC 17_18-BCC_FUL-M-Tu-W-Th-01";
var result = entityList
   .Where(entity => entity != null
       && entity.trip_update != null
       && entity.trip_update.trip != null
       && entity.trip_update.trip.trip_id == horribleString);

If you use a modern version of C# you can use the null conditional operator. which makes your query better readable:

 var result = entityList
    .Where(entity => entity?.trip_update?.trip?.trip_id == horribleString); 
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • I wonder what is wrong with my answer that I get downvoted. At least give some feedback, so I can improve my answer. This is not encouraging to answer people's questions event if I think RTFM – Harald Coppoolse Feb 02 '18 at 07:28