3

I have Room and Bed EF classes which each of Room have some Beds.when I use this LINQ statement:

IEnumerable<Room> room=...
if (room == null || !room.Any())
    return null;
return room.SelectMany(r=>r.Beds); 

give me this error:

Object reference not set to an instance of an object.

in return line.

4
  • 6
    So maybe there is at least 1 room with Beds==null Commented Jul 13, 2013 at 19:08
  • 2
    Thinking isn't good enough, you should know. Or find out. Commented Jul 13, 2013 at 19:11
  • @HenkHolterman I only have one Room with two Beds in it Commented Jul 13, 2013 at 19:15
  • 2
    Your code looks OK and shouldn't give this error. So try to reproduce it in a small but complete program we can run as well. Most likely you'll find the cause yourself along the way. Commented Jul 13, 2013 at 19:20

5 Answers 5

17

One of your rooms in the enumerable is null. Do this:

return room.Where(r => r != null)
           .SelectMany(r => r.Beds);
Sign up to request clarification or add additional context in comments.

2 Comments

for me it was return room.Where(r => r.Beds != null) .SelectMany(r => r.Beds);
Anyone knows why this happens? It works when doing room.Select(x => x.beds) when has a null on it, why selectmany doesnt?
2

I found out that my Collection of Rooms were not null and also none of Room's Beds were null. problem was that at least one item of my Room Collection was null. so according to YK1's answer I should use:

return room.Where(r => r != null).SelectMany(r => r.Beds);

Comments

1

The error can only happen when a Room has Beds == null.

You state: "I only have one Room with two Beds in it" but the question also mentions EF.

So the problem is in the ... in IEnumerable<Room> room=....

When your EF query uses lazy loading the Beds property will be null even if there are records.

For a complete solution you'll have to post all details about the EF part: Code|DB first, the query, which type Context class, EF version etc.

With the latest versions of EF this kind of problem is rare, my guess is that you have a ToList() in the query where you shouldn't.

2 Comments

but why if(!room.Any(x => x.Beds == null)) give me same error?
I don't really know. Unable to tell from what you posted.
0

You can try to use count too, like this:

IEnumerable<Room> room=...
if (room == null)    // Check for nulls
       return null;
else if (room.count() == 0)     // Check if empty
       return null;
else if(!room.Any(x => x.Beds == null)      // Check if there is no null Beds
       return room.SelectMany(r=>r.Beds);

2 Comments

A lot of checking but what could be the real cause?
The count() == 0 would be to replace the !room.Any() i believe.
0

Please try this..

return room.Where(r => r != null & r.Beds != null).SelectMany(r => r.Beds);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.