I'm sure I've got something wrong here but nobody in my team can explain what it is. My goal is to be able to populate a listView item from a button click in another form however a null reference exception is thrown when passing the variables to form1 (System.NullReferenceException: 'Object reference not set to an instance of an object.')
here is my code: Form1:
AddRec addRec = new AddRec();
public AddRec AddRec { get; set; }
public static string viewItem1, viewItem2, viewItem3 = "";
public void YesClick()
{
ListViewItem lvi = new ListViewItem(viewItem1);
lvi.SubItems.Add(viewItem2);
lvi.SubItems.Add(viewItem3);
listView1.Items.Add(lvi);
}
Form2(AddRec):
private void btnAdd_Click(object sender, EventArgs e)
{
name = txtNameF.Text.ToLower() + " " + txtNameS.Text.ToLower();
if (name != "")
{
num = txtNum.Text;
mobileNum = txtModNo.Text;
Form1.viewItem1 = name;
Form1.viewItem2 = num;
Form1.viewItem3 = mobileNum;
Form1.YesClick();
txtNameF.Text = "";
txtNameS.Text = "";
txtNum.Text = "";
txtModNo.Text = "";
}
else
{
MessageBox.Show("Fields Required", "Warning");
}
}
The idea is that the add button on AddRec populates a listView in Form1 with the text from text boxes in AddRec. The exception is thrown on the Form1.YesClick()