S
shapper
Hello,
I have the following loop and error message:
foreach (Theme theme in Profile.Collaborator.Themes) { ...
And I get the following error:
Object reference not set to an instance of an object.
I know I get this error because Themes is null ... I just don't know
how to solve it without needing to do:
Profile.Collaborator.Themes = new List<Theme>();
So here is my code before I use the loop:
MyProfile = new ProfileLeaf(helper);
helper is an instance of ProfileHelper which includes the properties
ProfileLeaf needs.
ProfileLeaf is as follows:
public class ProfileLeaf {
public Collaborator Collaborator { get; set; }
public Contact Contact { get; set; }
public ProfileLeaf() {
this.Collaborator = new Collaborator();
this.Contact = new Contact();
}
public ProfileLeaf(ProfileHelper profile) {
// this.Collaborator = profile.Collaborator == null ?
profile.Collaborator : new Collaborator();
this.Collaborator = profile.Collaborator;
this.Contact = profile.Contact;
}
}
Note the commented line! If I use this I am able to solve the problem.
But what if Collaborator is not empty but only the Themes inside
it ...
Going on. Collaborator class:
public class Collaborator {
public string CurriculumVitae { get; set; }
public List<Theme> Themes { get; set; }
public Collaborator() {
this.Themes = new List<Theme>();
}
}
Theme is a class with two properties:
public class Theme {
public Subject Subject { get; set; }
public List<Level> Levels { get; set; }
}
Basically, I would like to integrate on my code, in each class, a way
to prevent this kind of errors.
How should I do this?
Thanks,
Miguel
I have the following loop and error message:
foreach (Theme theme in Profile.Collaborator.Themes) { ...
And I get the following error:
Object reference not set to an instance of an object.
I know I get this error because Themes is null ... I just don't know
how to solve it without needing to do:
Profile.Collaborator.Themes = new List<Theme>();
So here is my code before I use the loop:
MyProfile = new ProfileLeaf(helper);
helper is an instance of ProfileHelper which includes the properties
ProfileLeaf needs.
ProfileLeaf is as follows:
public class ProfileLeaf {
public Collaborator Collaborator { get; set; }
public Contact Contact { get; set; }
public ProfileLeaf() {
this.Collaborator = new Collaborator();
this.Contact = new Contact();
}
public ProfileLeaf(ProfileHelper profile) {
// this.Collaborator = profile.Collaborator == null ?
profile.Collaborator : new Collaborator();
this.Collaborator = profile.Collaborator;
this.Contact = profile.Contact;
}
}
Note the commented line! If I use this I am able to solve the problem.
But what if Collaborator is not empty but only the Themes inside
it ...
Going on. Collaborator class:
public class Collaborator {
public string CurriculumVitae { get; set; }
public List<Theme> Themes { get; set; }
public Collaborator() {
this.Themes = new List<Theme>();
}
}
Theme is a class with two properties:
public class Theme {
public Subject Subject { get; set; }
public List<Level> Levels { get; set; }
}
Basically, I would like to integrate on my code, in each class, a way
to prevent this kind of errors.
How should I do this?
Thanks,
Miguel