Method throws null reference exception after returning non

I have a service method that very simply gets the information for all stores in the database. It maps the stores from EF using Auto Mapper, and returns a generic response of type StoreDTO (a simple POCO).

The problem is this: the method executes just fine, I step through all the way to the end. Every property in response has a value, nothing is null. The list is populated with items, the items in the list are valid, etc etc.

But the following code throws a NullReferenceException as soon as GetAllStores returns:

ListResponseDTO<StoreDTO> allStores = Services.Stores.Stores.GetAllStores();

EDIT: Here is a screenshot of the debugger, right when it is returning. You can see in the watch window that the values look kosher: http://i.imgur.com/rd853.png

Any help is greatly appreciated. Here is the code from the method:

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

Here is the generic DTO definition:

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

In case you were wondering, ResponseDTO has two properties:

bool Success

string Message

Here is the exception details, I'm afraid it's not too helpful:

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:UsersjlucasDocumentsVisual Studio 2010PLM Source ControlUtilitiesInfinityInterfaceInfinityDefault.aspx.cs:line 96
  InnerException: 

Can you put a where clause so you return only stores that you are sure they have all fields, and see if the problem persists?

This some times happen because, somewhere in your data set, you have missing data and during debug you don't see it.

You could also put another try catch that boxes the Mapper call and see if something is happening there.

This are more suggestions then an answer.

链接地址: http://www.djcxy.com/p/68916.html

上一篇: 为什么会发现一个类型的初始化器抛出一个NullReferenceException?

下一篇: 方法在返回非之后抛出空引用异常