I am experiencing a problem while choosing Yes on my MessageBox with buttons Yes or No.
Object reference not set to an instance of an object.
From line:
AddEntryWindow addWindow = new AddEntryWindow
(this, storedAuth.UserName, storedAuth.Password);
I don’t understand what’s the problem since few lines after this, is the same statement. How can I fix this?
Fixed
private void tsmiAddEntry_Click(object sender, EventArgs e)
{
if (storedAuth == null)
{
DialogResult result = MessageBox.Show
("You must log in before you add an entry."
+ Environment.NewLine + "You want to authenticate?",
"Information", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
AuthenticationWindow authWindow =
new AuthenticationWindow();
authWindow.ShowDialog();
storedAuth = authWindow.Result;
AddEntryWindow addWindow = new AddEntryWindow
(this, storedAuth.UserName, storedAuth.Password);
addWindow.ShowDialog();
}
}
else
{
AddEntryWindow addWindow = new AddEntryWindow
(this, storedAuth.UserName, storedAuth.Password);
addWindow.ShowDialog();
}
}
Look this statement
if (storedAuth == null)
and you accessing property of a null object. if it is necessary object then assign value to this object then access UserName and Password.
this is reason of error.. you should not use storedAuth.UserName, storedAuth.Password) in the following statement. use some default value like “” or “Default”
AddEntryWindow addWindow = new AddEntryWindow
(
this, storedAuth.UserName, storedAuth.Password);addWindow.ShowDialog();