C# 方法在返回非null值后引发null引用异常
我有一个服务方法,它非常简单地获取数据库中所有存储的信息。它使用自动映射器从EF映射存储,并返回类型为StoreDTO(简单POCO)的通用响应 问题是:这个方法执行得很好,我一步一步走到最后。C# 方法在返回非null值后引发null引用异常,c#,entity-framework,automapper,nullreferenceexception,dto,C#,Entity Framework,Automapper,Nullreferenceexception,Dto,我有一个服务方法,它非常简单地获取数据库中所有存储的信息。它使用自动映射器从EF映射存储,并返回类型为StoreDTO(简单POCO)的通用响应 问题是:这个方法执行得很好,我一步一步走到最后。response中的每个属性都有一个值,没有任何内容是空的。列表中填充了项目,列表中的项目有效,等等 但以下代码在GetAllStores返回时立即抛出NullReferenceException: ListResponseDTO<StoreDTO> allStores = Services.
response
中的每个属性都有一个值,没有任何内容是空的。列表中填充了项目,列表中的项目有效,等等
但以下代码在GetAllStores
返回时立即抛出NullReferenceException:
ListResponseDTO<StoreDTO> allStores = Services.Stores.Stores.GetAllStores();
是否可以放置where子句,以便只返回确定所有字段都存在的存储,并查看问题是否仍然存在 这种情况有时会发生,因为在数据集中的某个地方,您缺少数据,并且在调试过程中看不到数据 您还可以放置另一个try-catch来阻止Mapper调用,并查看那里是否发生了什么事情
这是更多的建议,而不是答案。尝试删除Try/catch,看看有什么效果happens@DJKRAZE:GetAllStores方法是第二段代码。能否发布异常的完整堆栈跟踪?能否显示调用
服务.Stores.Stores.GetAllStores()的更多上下文?堆栈跟踪是否在内部异常中有任何内容,或者它实际上是在那里停止的?大概是GetDrawersForUser()
是一个方法,行ListResponseDTO allStores=Services.Stores.Stores.GetAllStores()代码>在里面?我还假设,从你的屏幕截图来看,当你在那个点上单击“跳过”时,它会返回给家长,这就是它立即抛出错误的时候?是否有机会看到更多的GetDrawersForUser()
方法来了解它是否与您如何调用它有关?
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;
}
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;
}
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: