c# – Object losing reference when assigning it to Session? – Education Career Blog

I am facing a very strange issue. I get an error Object Reference Not set to instance of object on the server while the code runs fine on my dev machine. The strange part is the line number where the code is throwing error (as appears in the Stack trace)

DemoUser demoUser = new DemoUser();

if (!Request.QueryString.AllKeys.Contains("name"))
{
  playerName = usr.NinjaName;
  demoUser.Email = usr.UserEmail;
  demoUser.UserPicture = usr.UserPicture;
}
else
{
  playerName = Request.QueryString"name";
  demoUser.Email = String.Empty;
  demoUser.UserPicture = "http://graph.facebook.com/1/picture";
}         

demoUser.EntryDateTime = DateTime.Now;
demoUser.Name = playerName;

Session"DemoUser" = demoUser;

imgPlayer.ImageUrl = demoUser.UserPicture; // This is throwing error. 

If that line is throwing error then the demoUser object is getting lost when I assign it to Session. Is that right?

,

Are you sure that demoUser is the null reference and not imgPlayer?

,

Try:

demoUser = (DemoUser)Session"DemoUser"; 

imgPlayer.ImageUrl = demoUser.UserPicture;

Leave a Comment