Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:37:36+00:00 2026-05-31T18:37:36+00:00

Here is the code: public IEnumerable<UserSummary> getUserSummaryList() { var db = new entityContext(); List<UserSummary>

  • 0

Here is the code:

public IEnumerable<UserSummary> getUserSummaryList()
{
    var db = new entityContext();
    List<UserSummary> model = new List<UserSummary>();

    List<aspnet_Users> users = (from user in db.aspnet_Users
                 select user).ToList<aspnet_Users>();
    foreach (aspnet_Users u in users) //u is always null while users is a list that contains 4 objects
    {
        model.Add(new UserSummary()
        {
            UserName = u.UserName,
            Email = u.aspnet_Membership.Email,
            Role = Roles.GetRolesForUser(u.UserName).First(),
            AdCompany = u.AD_COMPANIES.ad_company_name != null ? u.AD_COMPANIES.ad_company_name : "Not an Advertiser",
            EmployeeName = u.EMPLOYEE.emp_name != null ? u.EMPLOYEE.emp_name : "Not an Employee"
        });
    }

    return model;
}

For some reason the u variable in the foreach loop is always null. I’ve stepped through the code and the users collection is always populated. The table entity for db.aspnet_Users is the users table that comes with asp.net membership services. I’ve only added a couple associations to it.

edit : image of debugger

debugger

Here is the stacktrace:

[NullReferenceException: Object reference not set to an instance of an object.]
   OutAd.Models.AccountMembershipService.getUserSummaryList() in C:\Users\stephen\Desktop\outad\OutAd\OutAd\Models\AccountModels.cs:301
   OutAd.Controllers.AdminController.UserList() in C:\Users\stephen\Desktop\outad\OutAd\OutAd\Controllers\AdminController.cs:78
   lambda_method(Closure , ControllerBase , Object[] ) +96
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.Mvc.<>c__DisplayClassa.<EndProcessRequest>b__9() +23
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +12
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +38
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action) +65
   System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) +71
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1072

[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +3049403
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +77
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +28
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +22
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +497
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +88
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName) +10
   ASP._Page_Views_Admin_Users_cshtml.Execute() in c:\Users\stephen\Desktop\outad\OutAd\OutAd\Views\Admin\Users.cshtml:7
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969201
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:37:37+00:00Added an answer on May 31, 2026 at 6:37 pm

    I’m going to guess that u is not null in itself, but one of it’s properties is null, or the Roles.GetRolesForUser(u.UserName) call returns null. If that is the case then when you try to access the members of those null values you get the NullReferenceException.

    Example:

    public class Person
    {
        public Address HomeAddress { get; set; }
    }
    
    public class Address
    {
        public String StreetAddress { get; set; }
    }
    
    public void SomeFunc()
    {
        var person = new Person();
    
        //NullReferenceException because HomeAddress is null.
        //NOT because person is null...
        var address = person.HomeAddress.StreetAddress;
    }
    

    You can verify this by hovering over each item in the Debugger to ensure that it is not null, but I bet you one of those objects is. To ensure that your GetRolesForUser method is not returning null, simply call that in the immediate window and see what you get back. If you get back null, there’s your issue.

    Alternatively, just throw each of the right hand assignments into your local watch window and one of them will present itself as the issue.


    Oh, and a few other things…

    1. If you are planning to return an IEnumerable<T> do not create a list and then return that list, instead just yield return your objects.

    2. Use null coalescing operators when necessary.

    3. Use a using statement for your IDisposable objects.

    Fixed version:

    public IEnumerable<UserSummary> getUserSummaryList()
    {
        using (var db = new EntityContext())
        {
            foreach (aspnet_Users user in users)
            {
                // Retrieve the username (with logic if it is null or empty)
                var username = user.UserName;
    
                // Retrieve the email (with logic if it is null or empty)
                var email = (user.aspnet_Membership != null)
                    ? user.aspnet_Membership.Email ?? String.Empty
                    : String.Empty;
    
                // Retrieve the role (with logic if it is null)
                var roles = Roles.GetRolesForUser(username);
                var role = (roles != null) ? roles.FirstOrDefault() : null;
    
                // Retrieve the Ad Company (with logic if it is null)
                var adCompany = (user.AD_COMPANIES != null)
                    ? user.AD_COMPANIES.ad_company_name ?? "Not an Advertiser"
                    : "Not an Advertiser";
    
                var empName = (user.EMPLOYEE != null)
                    ? user.EMPLOYEE.emp_name ?? "Not an Employee"
                    : "Not an Employee";
    
                yield return new UserSummary
                    {
                        UserName     = username,
                        Email        = email,
                        Role         = role,
                        AdCompany    = adCompany,
                        EmployeeName = empName,
                    };
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Server-Side code works: [OperationContract] public IEnumerable<object> GetBooks() { var people = new
I have a code similar to this: public static IEnumerable<T> ParseInput<T>(string input) { var
I need to return an IEnumerable. Here is the working code: public IEnumerable<String> ReturnStudentsAsString(int
I've gotta be missing something simple here. Take the following code: public IEnumerable<int> getInt(){
Here is the code for the mapper: public IEnumerable<GetQuestionsContract> Map(IEnumerable<XmlNode> nodes, XmlNamespaceManager namespaceManager) {
In the following code: public class A { public A():this(null){} public A(string b){/*code here*/}
Here is my code : public static void main(String[] args) { // System.setProperty( //
Here is my code: public class Test { static { main(null); } public static
So here is my code: public MyClass (int y) { super(y,x,x); //some code }
So here's my code : public class SetWritable<T extends Writable> implements Writable { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.