[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

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!
0
VBBRett
Asked:
VBBRett
  • 6
  • 4
1 Solution
 
abelCommented:
> 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
0
 
VBBRettAuthor Commented:
Would I put this right above the code where I have the error?
0
 
VBBRettAuthor Commented:
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)
        {

        }
    }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
VBBRettAuthor Commented:
Does anybody have an idea?
0
 
abelCommented:
Ah, you are on C#, not on VB, you didn't specify the language in the question. The code I showed you was VB and you are programming in C# (btw: please use the code snippet window for larger snippets of code). So, in C# this would yield the same, but the syntax is of course slightly different:

MembershipUser myUser = Membership.GetUser();
if(myUser == null)
{
    // there's no user, let's call it "<NOT_LOGGED_IN>"
    // you probably want to replace this line:
    drLog.UserName = "<NOT_LOGGED_IN>";
}
else
{
    // there's a user:
    drLog.UserName = myUser.UserName;
}

Open in new window

0
 
VBBRettAuthor Commented:
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!
0
 
abelCommented:
Like I said earlier, on the line that's shining up with the error. In other words:

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

0
 
VBBRettAuthor Commented:
It worked, you are the MAN!  Thank you so much!!
0
 
VBBRettAuthor Commented:
Your answer helped me very much, once again.  Thank you!
0
 
abelCommented:
You're welcome, glad I could be of some help ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now