Profile. What am I doing wrong?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am using a custom Profile provider as follows:

public class Profile : ProfileBase {

[SettingsAllowAnonymous(false)]
public Bio Bio { get; set; }

[SettingsAllowAnonymous(true)]
public Visitor Visitor { get; set; }

public static Profile Get() {
return Create(Membership.GetUser().UserName) as Profile;
}
public static Profile Get(string username) {
return Create(username) as Profile;
}

}

Visitor is a class has follows:

[Serializable()]
public class Visitor {
public CultureInfo Culture { get; set; }
}

On my web page I am overriding InitializeCulture, by getting the value
of Visitor.Culture:

1 protected override void InitializeCulture() {
2 base.InitializeCulture();
3 CultureInfo culture = Profile.Get().Visitor.Culture;
4 if (culture != null) {
5 Thread.CurrentThread.CurrentCulture = culture;
6 Thread.CurrentThread.CurrentUICulture = culture;
7 }
8 }

Because of code line 3 I always get an error on my profile provider:

1 public static Profile Get() {
2 return Create(Membership.GetUser().UserName) as Profile;
3 }

The error is on line 2 and says:

"Object reference not set to an instance of an object. Use the new
keyword to create an object instance."

What am I doing wrong?

Thanks,

Miguel
 
Hello,

I am using a custom Profile provider as follows:

  public class Profile : ProfileBase {

    [SettingsAllowAnonymous(false)]
    public Bio Bio { get; set; }

    [SettingsAllowAnonymous(true)]
    public Visitor Visitor { get; set; }

    public static Profile Get() {
      return Create(Membership.GetUser().UserName) as Profile;
    }
    public static Profile Get(string username) {
      return Create(username) as Profile;
    }

  }

Visitor is a class has follows:

  [Serializable()]
  public class Visitor {
    public CultureInfo Culture { get; set; }
  }

On my web page I am overriding InitializeCulture, by getting the value
of Visitor.Culture:

1        protected override void InitializeCulture() {
2          base.InitializeCulture();
3          CultureInfo culture = Profile.Get().Visitor.Culture;
4          if (culture != null) {
5            Thread.CurrentThread.CurrentCulture = culture;
6            Thread.CurrentThread.CurrentUICulture = culture;
7          }
8        }

Because of code line 3 I always get an error on my profile provider:

1        public static Profile Get() {
2          return Create(Membership.GetUser().UserName) as Profile;
3        }

The error is on line 2 and says:

"Object reference not set to an instance of an object. Use the new
keyword to create an object instance."

What am I doing wrong?

Thanks,

Miguel

I changed my code to the following and debbuged it:

Profile profile = Profile.Get(User.Identity.Name);
CultureInfo culture = profile.Visitor.Culture;

Now profile is defined but Visitor is null ... So now I have the same
error, "Object reference not set to an instance of an object." on code
line 2.

The user is an anonymous user and it is the first time that accesses
the web site so it is normal that none of the profile properties have
been defined.

How should I prevent this error?

Thanks,

Miguel
 
Back
Top