Link to home
Start Free TrialLog in
Avatar of VBBRett
VBBRett

asked on

Null Reference not set to an instance of an object

I am working on an asp.net site and the problem I am getting is an error saying the following:

'NULL REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT'

and below is the line of code where I get this problem:

            drLog.UserName =  Membership.GetUser().UserName;

The problem is when I set the line of code above to be  the following which is:

drLog.UserName = new Membership.GetUser().UserName;

I get an error that says the following:
System.Web.Security.Membership.GetUser(object, bool)' is a 'method' but is used like a 'type'

What can I do to get past this problem?  Thanks!
Avatar of abel
abel
Flag of Netherlands image

> System.Web.Security.Membership.GetUser(object, bool)' is a 'method' but is used like a 'type'

that is because of the new keyword on the method GetUser and that cannot be done. Try the following:

Dim myUser as MembershipUser = Membership.GetUser()If myUser Is Nothing Then    ' no user found that's logged inElse    ' there's a user, do something    drLog.UserName = myUser.UserNameEnd If
Avatar of VBBRett
VBBRett

ASKER

Would I put this right above the code where I have the error?
Avatar of VBBRett

ASKER

So how would I do this given that I have the following as code on my page?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using SMMC.IT.BusinessObjects;
using SMMC.IT.DataDefinition.EntityObjects;


namespace SMMC.IT.Web.UI
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception oErr = Server.GetLastError().GetBaseException();
            CommonSystem oCommon = new CommonSystem();
           
            CommonDataSet.ErrorLogDataTable dtLog = new CommonDataSet.ErrorLogDataTable();
            CommonDataSet.ErrorLogRow drLog = (CommonDataSet.ErrorLogRow) dtLog.NewErrorLogRow();
           

            drLog.UserName = Membership.GetUser().UserName;
            drLog.ErrorMsg = oErr.Message.ToString();
            drLog.StackTrace = oErr.StackTrace.ToString();
            dtLog.AddErrorLogRow(drLog);
            oCommon.InsertErrorLog(dtLog);

            Server.ClearError();
            System.Web.HttpContext.Current.Response.Redirect("ErrorPage.htm", true);
           
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}
Avatar of VBBRett

ASKER

Does anybody have an idea?
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VBBRett

ASKER

So where would I place this code that you sent me in the large block of code that I sent out?  Please let me know, thank you!
Like I said earlier, on the line that's shining up with the error. In other words:

    drLog.UserName = Membership.GetUser().UserName;

Avatar of VBBRett

ASKER

It worked, you are the MAN!  Thank you so much!!
Avatar of VBBRett

ASKER

Your answer helped me very much, once again.  Thank you!
You're welcome, glad I could be of some help ;)