null object cannot be converted to a value type

Hello everybody,

I am writing a windows application using C#.

I have a form, which calls another form with this code:

            using (NewForm nf = new NewForm())
                //My initialization

                //My Post Dialog Code

             //This is point ONE
            //This is point TWO

Between point ONE and point TWO, I get a MessageBox saying "null object cannot be converted to a value type." without any additional information. There is an OK button and the Close Box.

- I catch exceptions in both forms
- It is obvious that there is something at the disposal of NewForm
- I get this message also with another form, but many others run without error

Thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Congratulations on placing 'usings' around your Forms!  It's rare to see someone do it correctly.
There's nothing wrong with the code you posted.  You need to show us the .Designer code before we can tell you what is wrong.
taytekAuthor Commented:
Thanks for the compliment. I took a look at the .Designer and that lead me to find the source of the problem.

My bad, my mistake. The message is actually the exception message that I am catching. Here is the code:

private void contractComboBox_SelectedIndexChanged(object sender, EventArgs e)
                this.tblActivityTableAdapter.FillByContract(this.createRequestDataSet.tblActivity, new System.Nullable(((int)(System.Convert.ChangeType(contractComboBox.SelectedValue, typeof(int))))));
                this.tblActivityDataGridView.Enabled = true;
            catch (System.Exception ex)

The code runs normally, except that it catches the exception on disposal  of the components, before the disposal of the form:

protected override void Dispose(bool disposing)
            if (disposing && (components != null))
                components.Dispose();  //Here I get the exception message

But, still cannot find the answer why is this event being trigger on disposal?

Is there a way to overcome this exception without catching it?

When the message box comes back up.  Use the 'Debug-Break' command to see where in the code this is happening.  
Then take a look at the Call Stack.  This is going to give you the path that the code went down to call this method.
I'm sure the real culprit is in there.
Since you got kudos for placing the 'usings' around your form. I'm going to have to deduct some now for not using them in your TableAdapters!
Also, don't catch 'Exception', use specific exceptions.
taytekAuthor Commented:
Ok, this is an "Invalid Cast Exception" at this point:

this.tblActivityTableAdapter.FillByContract(this.createRequestDataSet.tblActivity, new System.Nullable<int>(((int)(System.Convert.ChangeType(contractComboBox.SelectedValue, typeof(int))))));

because "contractComboBox.SelectedValue = null"

Handling this is trivial.

But, please make this clear to me, how is this event being triggered at disposal?

My Call Stack is:
-    CreateRequest.contractComboBox_SelectedIndexChanged(object sender = {System.Windows.Forms.ComboBox, Items.Count: 1}, System.EventArgs e = {System.EventArgs})
-           [External Code]      
-     CreateRequest.Dispose(bool disposing = true) Line 18 + 0xc bytes
-           [External Code]      
-      RequestMaterials.openPanelButton_Click(object sender = {Text = "Open Activities Panel"}, System.EventArgs e = {X = 117 Y = 6 Button = Left}) Line 50 + 0x3c bytes      
-           [External Code]
-           Program.Main() Line 23 + 0x1a bytes

My *only* guess to why is if you have the list open when the close button is clicked.
Other than that, I'm stumped on why Dispose is calling that method.
You can call 'IsDisposed' in the method to keep it from trying to fill a database at that point.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.