0

By following this tutorial Here I got to succesfully convert the User Id to int so i can use Linq with operations done on users in my application.

Everything worked fine here, but after creating default users in startup.cs like this:

public async Task CreateUsersAndRoles(IServiceScope serviceScope){

    var userManager = serviceScope.ServiceProvider.GetService<UserManager>();
    var roleManager = serviceScope.ServiceProvider.GetService<RoleManager>();

    //Creating Admin role
    await roleManager.CreateAsync(new IdentityRole("TopUser"));
    var TopUsers = new ApplicationUser { UserName = "xxxxx@xx.com", 
                                          Email = "xxx@xxxx.com",  
                                          };


    await userManager.CreateAsync(adminUser, "xxxxxx");
    await userManager.AddToRoleAsync(TopUsers, "TopUser");
}

I am getting an error in CreateUsersAndRoles(serviceScope).Wait(); that is not clear and I don't know why the compiling is failing as eveything seems to be logically working.

Here is the error:

An exception of type 'System.AggregateException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'One or more errors occurred.' Inner exceptions found, see $exception in variables window for more details. Innermost exception System.NullReferenceException : Object reference not set to an instance of an object.

What am I doing wrong? And how do i find the error that is causing the code to fail?

Johnny
  • 8,045
  • 2
  • 24
  • 33
  • 5
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Bons Mar 11 '18 at 10:17
  • 2
    Cause: *Innermost exception System.NullReferenceException : Object reference not set to an instance of an object.* – Peter Bons Mar 11 '18 at 10:18
  • 1
    "What am i doing wrong?" -> Trying to use something before assigning a value to it. "And how do i find the error that is causing the code to fail?" -> Use the [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – DiplomacyNotWar Mar 11 '18 at 10:19
  • So as i understand, that means i am trying to create a new user without assigning a value to their Id, which doesn't make sense because i keep getting the same error even after assigning an Id to the user i am creating. What is exactly being null here? – SigridA Mar 11 '18 at 10:29
  • @SigridA `serviceScope` might be, `serviceScope.ServiceProvider` might be, the result of `serviceScope.ServiceProvider.GetService<>` might be, the code within one of the methods you're calling might be causing the exception (you haven't provided a full stack trace). – DiplomacyNotWar Mar 12 '18 at 01:08

0 Answers0