我有一个服务方法,它可以非常简单地获取数据库中所有存储的信息。它使用Auto Mapper映射EF的商店,并返回类型为StoreDTO(简单POCO)的通用响应。返回非空值后抛出空引用异常
问题是这样的:该方法执行得很好,我一直走到最后。 response
中的每个属性都有一个值,没有任何值为空。该列表填充项,在列表中的项目是有效的,等等等等
但下面的代码,只要GetAllStores
回报抛出一个NullReferenceException:
ListResponseDTO<StoreDTO> allStores = Services.Stores.Stores.GetAllStores();
编辑:这里是的截图调试器,正确的时候它正在返回。您可以在观察窗口看到数值看起来像犹太教:http://i.imgur.com/rd853.png
任何帮助,非常感谢。下面是从方法的代码:
public static ListResponseDTO<StoreDTO> GetAllStores()
{
ListResponseDTO<StoreDTO> response = new ListResponseDTO<StoreDTO>("Get Stores not successful");
try
{
response.Items = new List<StoreDTO>();
using (DomainEntities db = new DomainEntities(Global.ConnectionString))
{
foreach (var IndividualStore in db.Stores)
{
Mapper.CreateMap<Store, StoreDTO>();
var IndividualStoreDTO = Mapper.Map<Store, StoreDTO>(IndividualStore);
response.Items.Add(IndividualStoreDTO);
}
}
response.Message = "Store(s) retrieved successfully";
response.Success = true;
}
catch (Exception ex)
{
Logging.Log("Get All Stores", response.Message + " " + ex.ToString(), Logging.LogPriority.Error, "Store Operations");
}
return response;
}
这里是通用的DTO的定义:
public class ListResponseDTO<DtoType> : ResponseDTO
{
public ListResponseDTO()
: base()
{
Items = new List<DtoType>();
}
public ListResponseDTO(string defaultMessage)
: base(defaultMessage)
{
Items = new List<DtoType>();
}
public List<DtoType> Items;
}
在你不知道的情况下,ResponseDTO
有两个属性:
bool Success
以下是异常的详细信息,恐怕这不是太大的帮助:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=Infinity
StackTrace:
at PLM.Infinity.Default.GetDrawersForUser() in C:\Users\jlucas\Documents\Visual Studio 2010\PLM Source Control\Utilities\InfinityInterface\Infinity\Default.aspx.cs:line 96
InnerException:
尝试删除try/catch并查看会发生什么 – 2012-02-20 14:37:58
同样的事情。它不会在方法中抛出异常,它只会在方法返回后抛出异常。 – lucrativelucas 2012-02-20 14:42:02
GetAllStores方法看起来像返回后返回Null的唯一方式是因为您要么重新创建它,要么您有一些奇怪的递归方法/属性调用来重置对象,为什么需要此行ListResponseDTO响应= new ListResponseDTO (“Get Stores not successful”); –
MethodMan
2012-02-20 14:55:27