So I have two forms. LibraryBookDialog.cs and MainForm.cs. I’m trying to pass an object from LibraryBookDialog.cs to Mainform.cs. Problem is, I get this error when I try to do so…
object reference not set to an instance of the object
Here are my two forms…
LibraryBookDialog.cs:
private LibraryBook book;
public LibraryBook Book
{
get { return book;}
set { book = value;}
}
private void buttonOk_Click(object sender, EventArgs e)
{
if (validateData())
{
try
{
Book.Title = textBoxTitle.Text;
Book.Author = textBoxAuthor.Text;
Book.CopyrightYear = Convert.ToInt32(textBoxCopyrightYear.Text);
this.DialogResult = DialogResult.OK;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There was an error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
and MainForm.cs
private void buttonNew_Click(object sender, EventArgs e)
{
LibraryBookDialogue dlg = new LibraryBookDialogue();
dlg.ShowDialog();
if (dlg.DialogResult == DialogResult.OK)
{
listBoxLibraryBooks.Items.Add(dlg.Book);
}
dlg.Dispose();
}
Why is this happening?
It looks like you never put a
LibraryBookinstance into theBookproperty.