Roles.GetRolesForUser() fails with a not very helpful error message.
What does this mean?
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 16: @{
Line 17: // Get a list of the roles the current user is in.
Line 18: string[] userRoles = Roles.GetRolesForUser();
Line 19: bool isInRequiredRole = false;
Line 20: if (userRoles.Contains("User"))
Source
File: c:\Users\bb\Documents\My Web Sites\try1\Admin\AdminMenu.cshtml Line:
18
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Security.Roles.GetRolesForUser(String username) +530
System.Web.Security.Roles.GetRolesForUser() +14
ASP._Page_Admin_AdminMenu_cshtml.Execute() in c:\Users\bigbird\Documents\My Web Sites\try1\Admin\AdminMenu.cshtml:18
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
System.Web.WebPages.WebPage.ExecutePageHierarchy() +156
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
This means "Roles" is not yet assigned with any object reference. It's still null when you try to call the method on it. You can place a break point at line #18 and check to see if "Roles" is null.
Check the code that initializes this variable and see if it's creating the object.
Hope this helps.
Please remember to click “Mark as Answer” on the post that helps you and to unmark it if a marked post does not actually answer your question.
Thank you!
----------------------
"Microsoft Community Contributor Award 2011"
Marked as answer by Dino He - MSFT on Aug 06, 2012 11:41 AM
wavemaster
Participant
1293 Points
1129 Posts
Object reference not set to an instance of an object - starter site roles
Jul 30, 2012 07:19 PM|LINK
Roles.GetRolesForUser() fails with a not very helpful error message.
What does this mean?
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 16: @{
Line 17: // Get a list of the roles the current user is in.
Line 18: string[] userRoles = Roles.GetRolesForUser();
Line 19: bool isInRequiredRole = false;
Line 20: if (userRoles.Contains("User"))
Source File: c:\Users\bb\Documents\My Web Sites\try1\Admin\AdminMenu.cshtml Line: 18
Stack Trace:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
santa_1975
Star
8574 Points
1499 Posts
Re: Object reference not set to an instance of an object - starter site roles
Jul 30, 2012 08:28 PM|LINK
Hi,
This means "Roles" is not yet assigned with any object reference. It's still null when you try to call the method on it. You can place a break point at line #18 and check to see if "Roles" is null.
Check the code that initializes this variable and see if it's creating the object.
Hope this helps.
Thank you!
----------------------
"Microsoft Community Contributor Award 2011"
wavemaster
Participant
1293 Points
1129 Posts
Re: Object reference not set to an instance of an object - starter site roles
Jul 30, 2012 09:07 PM|LINK
I found a potential problem with one of my tables called webpages_Roles.
I had typed one extra character in the RoleId column.
Since I cannot change the type I ended up deleting the table and starting from scratch.
Will let you know how it turns out.