null object cannot be converted to a value type

taytek
taytek used Ask the Experts™
on
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
                nf.ShowDialog();

                //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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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.

Author

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)
        {
            try
            {
                this.tblActivityTableAdapter.FillByContract(this.createRequestDataSet.tblActivity, new System.Nullable(((int)(System.Convert.ChangeType(contractComboBox.SelectedValue, typeof(int))))));
                this.tblActivityMaterialTableAdapter.Fill(this.createRequestDataSet.tblActivityMaterial);
                this.tblActivityDataGridView.Enabled = true;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

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
            }
            base.Dispose(disposing);
        }

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?


Commented:
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.

Author

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

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial