Link to home
Start Free TrialLog in
Avatar of taytek
taytek

asked on

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
                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
Avatar of psdavis
psdavis
Flag of United States of America image

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.
Avatar of taytek
taytek

ASKER

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?


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.
Avatar of taytek

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of psdavis
psdavis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial