Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod throws null reference exception after returning non-null value
    primarykey
    data
    text
    <p>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). </p> <p>The problem is this: the method executes just fine, I step through all the way to the end. Every property in <code>response</code> has a value, nothing is null. The list is populated with items, the items in the list are valid, etc etc.</p> <p>But the following code throws a NullReferenceException as soon as <code>GetAllStores</code> returns:</p> <pre><code>ListResponseDTO&lt;StoreDTO&gt; allStores = Services.Stores.Stores.GetAllStores(); </code></pre> <p>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: <a href="http://i.imgur.com/rd853.png" rel="nofollow">http://i.imgur.com/rd853.png</a></p> <p>Any help is greatly appreciated. Here is the code from the method:</p> <pre><code> public static ListResponseDTO&lt;StoreDTO&gt; GetAllStores() { ListResponseDTO&lt;StoreDTO&gt; response = new ListResponseDTO&lt;StoreDTO&gt;("Get Stores not successful"); try { response.Items = new List&lt;StoreDTO&gt;(); using (DomainEntities db = new DomainEntities(Global.ConnectionString)) { foreach (var IndividualStore in db.Stores) { Mapper.CreateMap&lt;Store, StoreDTO&gt;(); var IndividualStoreDTO = Mapper.Map&lt;Store, StoreDTO&gt;(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; } </code></pre> <p>Here is the generic DTO definition:</p> <pre><code>public class ListResponseDTO&lt;DtoType&gt; : ResponseDTO { public ListResponseDTO() : base() { Items = new List&lt;DtoType&gt;(); } public ListResponseDTO(string defaultMessage) : base(defaultMessage) { Items = new List&lt;DtoType&gt;(); } public List&lt;DtoType&gt; Items; } </code></pre> <p>In case you were wondering, <code>ResponseDTO</code> has two properties:</p> <p><code>bool Success</code></p> <p><code>string Message</code></p> <p>Here is the exception details, I'm afraid it's not too helpful:</p> <pre><code>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: </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload