0

This is a website that has been running with no problems for at least 8 years now, all of the sudden it starts doing this, in some pages we call on the C# code the line: Membership.GetUser().UserName

and sometimes it will return the error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

like if the user was not authenticated, but it is, if i refresh it happens the same but if i hold the F5 key for more than a second then it returns the data and it works just fine.

I have checked and the session cookie is there, and it works for a while then it starts doing it again. The server is performing.

Any ideas?

Here is one fragment of code that gives the error:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
      realNameLabel.Text= Membership.GetUser().UserName; 
    }
}

There is not much to it, simple code.

George
  • 163
  • 2
  • 13
  • [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) – Soner Gönül Mar 31 '15 at 08:08
  • 1
    And would be better to see your C# code as well. – Soner Gönül Mar 31 '15 at 08:08
  • 5
    @SonerGönül: some `NullReferenceExceptions` are really hard to find, even if you use the debugger. So sometimes we should not close questions immediately with this "duplicate"(what you haven't done yet) . Maybe this is a general issue which could be helpful for others once we know the reason. – Tim Schmelter Mar 31 '15 at 08:11
  • Session cookie has nothing to do with authentication - for that you have authentication cookie. For this kind of problem we kind of need to know what is your membership provider. – Ondrej Svejdar Mar 31 '15 at 08:11
  • Usually happens to me when i run my application using `LocalDb`. The reason in my case is that it always times out in first try. and works fine after page refresh. It would be better to see your code as @SonerGönül suggested – Zeeshan Mar 31 '15 at 08:11
  • @TimSchmelter Indeed. Without see OP's code, I _generally_ leave a comment to that link instead of directly vote to close. – Soner Gönül Mar 31 '15 at 08:15
  • edited the question, added the c# code basically just calling that line of code, whatever you do, even just set a string variable will give the error, in this case I set a label on the masterpage. – George Mar 31 '15 at 08:36
  • Just as a sanity check, are you saving any state to the thread? That could explain your transient problems. – Jon Bates Mar 31 '15 at 09:40

0 Answers0