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().UserN ame;
The problem is when I set the line of code above to be the following which is:
drLog.UserName = new Membership.GetUser().UserN ame;
I get an error that says the following:
System.Web.Security.Member ship.GetUs er(object, bool)' is a 'method' but is used like a 'type'
What can I do to get past this problem? Thanks!
'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().UserN
The problem is when I set the line of code above to be the following which is:
drLog.UserName = new Membership.GetUser().UserN
I get an error that says the following:
System.Web.Security.Member
What can I do to get past this problem? Thanks!
ASKER
Would I put this right above the code where I have the error?
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.Ent ityObjects ;
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(o bject sender, EventArgs e)
{
}
protected void Application_AuthenticateRe quest(obje ct sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
Exception oErr = Server.GetLastError().GetB aseExcepti on();
CommonSystem oCommon = new CommonSystem();
CommonDataSet.ErrorLogData Table dtLog = new CommonDataSet.ErrorLogData Table();
CommonDataSet.ErrorLogRow drLog = (CommonDataSet.ErrorLogRow ) dtLog.NewErrorLogRow();
drLog.UserName = Membership.GetUser().UserN ame;
drLog.ErrorMsg = oErr.Message.ToString();
drLog.StackTrace = oErr.StackTrace.ToString() ;
dtLog.AddErrorLogRow(drLog );
oCommon.InsertErrorLog(dtL og);
Server.ClearError();
System.Web.HttpContext.Cur rent.Respo nse.Redire ct("ErrorP age.htm", true);
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
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.Ent
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(o
{
}
protected void Application_AuthenticateRe
{
}
protected void Application_Error(object sender, EventArgs e)
{
Exception oErr = Server.GetLastError().GetB
CommonSystem oCommon = new CommonSystem();
CommonDataSet.ErrorLogData
CommonDataSet.ErrorLogRow drLog = (CommonDataSet.ErrorLogRow
drLog.UserName = Membership.GetUser().UserN
drLog.ErrorMsg = oErr.Message.ToString();
drLog.StackTrace = oErr.StackTrace.ToString()
dtLog.AddErrorLogRow(drLog
oCommon.InsertErrorLog(dtL
Server.ClearError();
System.Web.HttpContext.Cur
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
ASKER
Does anybody have an idea?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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().UserN ame;
drLog.UserName = Membership.GetUser().UserN
ASKER
It worked, you are the MAN! Thank you so much!!
ASKER
Your answer helped me very much, once again. Thank you!
You're welcome, glad I could be of some help ;)
that is because of the new keyword on the method GetUser and that cannot be done. Try the following: