Mike D.
Mike D.

Reputation: 13

New to C#. Object Reference not set to an instance of an object. nJupiter LDAP

HELP......Just moved to C# from vb and Im really lost with this.

var ldapmembershipUser = (LdapMembershipUser)System.Web.Security.Membership.GetUser("username");
var cnAttributeValues = ldapmembershipUser.Attributes["cn"];

Code bails at cnAttributeValues with Object reference not set to an instance of an object.

I dont know how to set it as a new ?? Object?

This is in reference to the nJupiter google controls.

Upvotes: 1

Views: 252

Answers (3)

Brian
Brian

Reputation: 2229

You need to check that the user exists first before trying to get the attributes.

Upvotes: 1

SLaks
SLaks

Reputation: 887867

That would mean that GetUser returned null, probably because there is no user with that name.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502696

Membership.GetUser(string) is documented to return:

A MembershipUser object representing the specified user. If the username parameter does not correspond to an existing user, this method returns Nothing.

So that's what's happening here - ldapmembershipUser is null because there's no such user. You should check for that and act accordingly:

// Note name casing change here to keep my sanity.
if (ldapMembershipUser == null)
{
    // Take appropriate action
}

Note that you could also have problems if GetUser returns a reference to an object which isn't an LdapMembershipUser.

Upvotes: 3

Related Questions