Reputation: 1305
I am trying to workout to see how I can access list items from a different class and to update the same, I have my code below for clarification.
class list
{
private List<Person> people;
public List<Person> People
{
get { return people; }
set { people = value; }
}
}
public partial class Form2 : Form
{
Person p = new Person();
list l = new list();
p.Name = textBox1.Text;
p.Streetaddress = textBox2.Text;
p.Email = textBox3.Text;
p.Birthday = dateTimePicker1.Value;
p.AdditionalNotes = textBox4.Text;
l.People.Add(p);
listView2.Items.Add(p.Name);
}
there is Person class which has instance variables Name, Streetaddress etc.
Getting an error
Nullreference exception was unhandled
Please help me..
Upvotes: 2
Views: 8688
Reputation: 4385
Basically, you have defined a property People in class list but have not initialized it. Just initialize it in the constructor of list class.
Can you try:
class list
{
private List<Person> people;
public List<Person> People
{
get { return people; }
private set { people = value;}
}
public list()
{
people = new List<Person>();
}
}
Upvotes: 2
Reputation: 22362
You need to instantiate the inner List<Person>
when the List class is instantiated. Otherwise it will be null.
class list
{
private List<Person> people = new List<Person>();
public List<Person> People
{
get { return people; }
set { people = value;}
}
}
Upvotes: 6