Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

null object cannot be converted to a value type

Posted on 2008-11-14
5
Medium Priority
?
2,401 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:taytek
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:psdavis
ID: 22959027
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.
0
 

Author Comment

by:taytek
ID: 22974082
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?


0
 
LVL 7

Expert Comment

by:psdavis
ID: 22975480
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.
0
 

Author Comment

by:taytek
ID: 22976089
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

0
 
LVL 7

Accepted Solution

by:
psdavis earned 500 total points
ID: 22976174
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.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question