Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

Null reference issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kuhrty - 06 Jul 2006 22:11 GMT
Hi,

I am creating a multi-winform project.  Everything has been working
fine, except after I perform an update to the database and decide to
Exit the winform after the update operation is complete.

I have error handling in my code but not on the dispose method.  I call

the dispose method exiting the application using the code below but
results in this error message
"An unhandled exception of type 'System.NullReferenceException'
occurred in system.management.dll
Additional information: Object reference not set to an instance of an
object."

I am may be using the dispose method incorrectly but I can't seem to
locate the issue.  Any advice or hints is greatly appreaciated.

private void mnuExit_Click(object sender, System.EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?","Exit",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Dispose();
}
}

protected override void Dispose( bool disposing )
{
if (!bOK)
{
if (MessageBox.Show("You have not updated the current data.  Would you
like to before exiting?",  "Exit",MessageBoxButtons.YesNo ) ==
DialogResult.Yes)
{
return;

}
}

if( disposing )
{
if (components != null)
{
components.Dispose();

}
}

base.Dispose( disposing );

}

Thanks in advance

Mark
FUnky - 07 Jul 2006 06:39 GMT
I don't think it is a good idea to call Dispose on menuExit_Click. Instead
call this.Close() there and inside the form_Closing event do your 'you have
not updated the current data' validation and set e.cancel = true, if the
user likes to update it.
William Sullivan - 07 Jul 2006 13:54 GMT
First, the creator of the window is responsible for calling Dispose on it,
not the window itself.  Additionally, don't put any logic in the Dispose
method.  You are only supposed to be releasing unmanaged resources held by
the form object there (and the only one held by forms (unless you add more)
is their window handle, which is released by calling base.Dispose()).  Move
all your logic into your button handler.  When the user indicates that the
form should be closed, use the Close() method to hide it from view; don't
call Dispose().  Whatever object opened the form in the first place will have
to determine whether or not its time to dispose the form.

> Hi,
>
[quoted text clipped - 52 lines]
>
> Mark

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.