1

I have created a different identity table MyUserInfo for my user profile where I store additional info such as Name, address etc. The idea is that if the user has not entered his / her data then he will be sent to another page where it can be done. The issue is that when I try to verify if the client has entered his name, address etc... i get an an error and I don't understand why:

Additional information: Object reference not set to an instance of an object.

this happens at the line where I try to check if the value of the User Name is null

if (currentUser.MyUserInfo.FirstName == null)

Here is the code:

string user = System.Web.HttpContext.Current.User.Identity.Name;

var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
store.AutoSaveChanges = false;

var currentUserId = User.Identity.GetUserId();
var manager = new UserManager<ApplicationUser>(store);
var currentUser = manager.FindById(User.Identity.GetUserId());

//check if the user is registered and what his role is
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated &&  System.Web.HttpContext.Current.User.IsInRole("Customer"))
{
    if (currentUser.MyUserInfo.FirstName == null) //offending line
    {
        Response.Redirect("EnterUserData.aspx");
    }
}
Michael Dunlap
  • 4,300
  • 25
  • 36
bluetxxth
  • 121
  • 2
  • 16
  • Did you try placing a breakpoint on the line where the error happens and checking the value of `currentUser` and `MyUserInfo` to make sure they're not null? Are you sure that if a non-null object is returned by `manager.FindById` that the MyUserInfo property will not be null? – Tony Vitabile Mar 27 '14 at 20:57
  • Well Either currentUser or currentUser.MyUserInfo is null, put a debug on it, find out which one and then suss out why. – Tony Hopkinson Mar 27 '14 at 20:58
  • @TonyHopkinson Hi Thank you ... currentUser is set but MyUserInfo is null – bluetxxth Mar 27 '14 at 21:12
  • Um just test for that as well then. if ((currentUser.MyUserInfo == null) || (currentUser.MyUserInfo.FirstName == null)) – Tony Hopkinson Mar 27 '14 at 21:15
  • I made it work, just like I said below MyUserInfo had to instantiated as well, I solved it by currentUser.MyUserInfo = new MyUserInfo(); then accessing the information currentUser.MyUserInfo.FirstName = FirstName.Text; and finally store.Context.SaveChanges(); just like in any other entity class – bluetxxth Mar 28 '14 at 09:38
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Albireo Dec 14 '15 at 09:10

1 Answers1

0

If manager.FindByID() returns null, you will get that exception when you try and reference properties in the currentUser object (which of course would also be null). In addition, currentUser.MyUserInfo could be null, causing the same issue.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
  • thank you for the help, I have been testing and verified that the currentUser object is not null, it is set. MyUserInfo is an (identity) entity class that makes a table where I store profile information about the user, and the corresponding table has been created. I still don’t understand how this is possible, I am just trying to set the user info that goes in the MyUserInfo table. How to make MyUserInfo not null? – bluetxxth Mar 27 '14 at 23:24
  • It was confusing but ... the thing that made it work was currentUser.MyUserInfo = new MyUserInfo(); – bluetxxth Mar 28 '14 at 09:35