Error Handling

  • Thread starter Thread starter Kimochi
  • Start date Start date
K

Kimochi

I got this error in my programm
how do i skip this error? thax

An unhandled exception of
type 'System.NullReferenceException' occurred in
system.windows.forms.dll

Additional information: Object reference not set to an
instance of an object.
 
try
{
// The offending code here.
}
catch(NullReferenceException)
{
}
 
this is my code

private void menuItem1_Click(object sender,
System.EventArgs e)
{
MessageBox.Show(ActiveMdiChild.Text);
}

ihave this function in my menu
it works if one of the form is open or active
if none of the child form is active it will prompt the
error. how do i handle it?
 
Perhaps, you can check whether any child form is active before accessing it:

if (ActiveMdiChild == null)
MessageBox.Show ("No active MDI child");
else
MessageBox.Show(ActiveMdiChild.Text);

this is my code

private void menuItem1_Click(object sender,
System.EventArgs e)
{
MessageBox.Show(ActiveMdiChild.Text);
}

ihave this function in my menu
it works if one of the form is open or active
if none of the child form is active it will prompt the
error. how do i handle it?
 
I have try that before but still cannot works
anyway ive try the

try and catch and it can skip the errors

thax you for the help
 
Back
Top