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
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
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_SelectedI ndexChange d(object sender, EventArgs e)
{
try
{
this.tblActivityTableAdapt er.FillByC ontract(th is.createR equestData Set.tblAct ivity, new System.Nullable(((int)(Sys tem.Conver t.ChangeTy pe(contrac tComboBox. SelectedVa lue, typeof(int))))));
this.tblActivityMaterialTa bleAdapter .Fill(this .createReq uestDataSe t.tblActiv ityMateria l);
this.tblActivityDataGridVi ew.Enabled = true;
}
catch (System.Exception ex)
{
System.Windows.Forms.Messa geBox.Show (ex.Messag e);
}
}
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?
My bad, my mistake. The message is actually the exception message that I am catching. Here is the code:
private void contractComboBox_SelectedI
{
try
{
this.tblActivityTableAdapt
this.tblActivityMaterialTa
this.tblActivityDataGridVi
}
catch (System.Exception ex)
{
System.Windows.Forms.Messa
}
}
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.
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.
ASKER
Ok, this is an "Invalid Cast Exception" at this point:
this.tblActivityTableAdapt er.FillByC ontract(th is.createR equestData Set.tblAct ivity, new System.Nullable<int>(((int )(System.C onvert.Cha ngeType(co ntractComb oBox.Selec tedValue, typeof(int))))));
because "contractComboBox.Selected Value = 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.contractComb oBox_Selec tedIndexCh anged(obje ct sender = {System.Windows.Forms.Comb oBox, Items.Count: 1}, System.EventArgs e = {System.EventArgs})
- [External Code]
- CreateRequest.Dispose(bool disposing = true) Line 18 + 0xc bytes
- [External Code]
- RequestMaterials.openPanel Button_Cli ck(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
this.tblActivityTableAdapt
because "contractComboBox.Selected
Handling this is trivial.
But, please make this clear to me, how is this event being triggered at disposal?
My Call Stack is:
- CreateRequest.contractComb
- [External Code]
- CreateRequest.Dispose(bool
- [External Code]
- RequestMaterials.openPanel
- [External Code]
- Program.Main() Line 23 + 0x1a bytes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.