简体   繁体   English

“对象引用未设置为对象的实例”即使我检查了 null

[英]"Object reference not set to an instance of an object" even when I checked null

I have Room and Bed EF classes which each of Room have some Bed s.when I use this LINQ statement:我有RoomBed EF 类,每个Room都有一些Bed s。当我使用这个 LINQ 语句时:

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. Object 引用未设置为 object 的实例。

in return line.return线。

One of your rooms in the enumerable is null. 可枚举中的一个房间是null。 Do this: 做这个:

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

I found out that my Collection of Room s were not null and also none of Room 's Beds were null . 我发现我CollectionRoom小号不null也没有的RoomBedsnull problem was that at least one item of my Room Collection was null . 问题是我的Room Collection中至少有一项是null so according to YK1 's answer I should use: 所以根据YK1的回答,我应该使用:

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

The error can only happen when a Room has Beds == null . 只有当房间有Beds == null时才会发生错误。

You state: "I only have one Room with two Beds in it" but the question also mentions EF. 你说:“我只有一​​间带两张床的房间”,但问题还提到EF。

So the problem is in the ... in IEnumerable<Room> room=... . 所以问题出现在... IEnumerable<Room> room=...

When your EF query uses lazy loading the Beds property will be null even if there are records. 当您的EF查询使用延迟加载时 ,即使有记录,Beds属性也将为null。

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. 要获得完整的解决方案,您必须发布有关EF部分的所有详细信息:Code | DB first,查询,类型Context类,EF版本等。

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. 使用最新版本的EF这种问题很少见,我的猜测是你在查询中有一个ToList() ,你不应该这样。

You can try to use count too, like this: 您也可以尝试使用count,如下所示:

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);

Please try this..请试试这个..

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我排除null,也将获得“对象引用未设置为对象的实例。” - Get “Object reference not set to an instance of an object.” even if I exclude null 空引用:对象引用未设置为对象的实例 - Null Reference : Object reference not set to an instance of an object 对象引用未设置为检查null的对象实例 - object reference not set to an instance of an object in check for null 未将对象引用设置为对象的实例,null - Object reference not set to an instance of an object, null “对象引用未设置为对象的实例”-但是没有什么是空的? - “Object reference not set to an instance of an object” - but nothing is null? 对象引用未设置为对象的实例(null DataSet ??) - Object reference not set to an instance of an object (null DataSet??) 未将对象引用设置为对象的实例,参数为null - Object reference not set to an instance of an object, parameter is null 对象引用错误,即使对象不为空 - Object reference error even when object is not null 获取对象引用未设置为对象实例的空引用对象的类型 - Get type of null reference object for Object reference not set to an instance of an object Crashlytics中记录“对象引用未设置为对象的实例”异常时,如何找出哪个对象引用为空? - How to find out which object reference is null, when an “Object reference not set to an instance of an object” exception is recorded in Crashlytics?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM