System.NullReferenceException: Check my solution someone, pls
-
I have combo box that is loaded dynamically. When the user select Misc from the list I want to unhide a text field. I recived the following error. System.NullReferenceException: Object reference not set to an instance of an object. at ACEPackaging.dlgEditSkidWeight.HideCarrerTextBox() etc etc etc Here is my code that is called when the user changes the selection on the combobox.
If CStr(cboCarrier.SelectedValue.ToString) = "99" Then
tbCarrierDesc.Visible = True
Else
tbCarrierDesc.Visible = False
End If
End IfI did a little research and said that this error is caused by the .ToString being Null or Nothing. So here is what I have added to correct the problem so there are no more errors. Please let me knwo if this will work.
If Not cboCarrier.SelectedValue.ToString Is Nothing Then 'added this line If CStr(cboCarrier.SelectedValue.ToString) = "99" Then tbCarrierDesc.Visible = True Else tbCarrierDesc.Visible = False End If End If
-
I have combo box that is loaded dynamically. When the user select Misc from the list I want to unhide a text field. I recived the following error. System.NullReferenceException: Object reference not set to an instance of an object. at ACEPackaging.dlgEditSkidWeight.HideCarrerTextBox() etc etc etc Here is my code that is called when the user changes the selection on the combobox.
If CStr(cboCarrier.SelectedValue.ToString) = "99" Then
tbCarrierDesc.Visible = True
Else
tbCarrierDesc.Visible = False
End If
End IfI did a little research and said that this error is caused by the .ToString being Null or Nothing. So here is what I have added to correct the problem so there are no more errors. Please let me knwo if this will work.
If Not cboCarrier.SelectedValue.ToString Is Nothing Then 'added this line If CStr(cboCarrier.SelectedValue.ToString) = "99" Then tbCarrierDesc.Visible = True Else tbCarrierDesc.Visible = False End If End If
ToString being Nothing wouldn't cause that problem... More likely, SelectedValue is the problem. Also, Using CStr on something that's already gone through ToString is redundant... A better way would be:
If Convert.ToString(cboCarrier.SelectedValue) = "99" Then
tbCarrierDesc.Visible = True
Else
tbCarrierDesc.Visible = False
End IfAs Convert.ToString can handle a null/Nothing argument, and would just return "".
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
I have combo box that is loaded dynamically. When the user select Misc from the list I want to unhide a text field. I recived the following error. System.NullReferenceException: Object reference not set to an instance of an object. at ACEPackaging.dlgEditSkidWeight.HideCarrerTextBox() etc etc etc Here is my code that is called when the user changes the selection on the combobox.
If CStr(cboCarrier.SelectedValue.ToString) = "99" Then
tbCarrierDesc.Visible = True
Else
tbCarrierDesc.Visible = False
End If
End IfI did a little research and said that this error is caused by the .ToString being Null or Nothing. So here is what I have added to correct the problem so there are no more errors. Please let me knwo if this will work.
If Not cboCarrier.SelectedValue.ToString Is Nothing Then 'added this line If CStr(cboCarrier.SelectedValue.ToString) = "99" Then tbCarrierDesc.Visible = True Else tbCarrierDesc.Visible = False End If End If
Assuming your ComboBox list does not hold any item that is Nothing, this should do:
tbCarrierDesc.Visible = cboCarrier.SelectedIndex >= 0 AND cboCarrier.SelectedValue.ToString = "99"
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
ToString being Nothing wouldn't cause that problem... More likely, SelectedValue is the problem. Also, Using CStr on something that's already gone through ToString is redundant... A better way would be:
If Convert.ToString(cboCarrier.SelectedValue) = "99" Then
tbCarrierDesc.Visible = True
Else
tbCarrierDesc.Visible = False
End IfAs Convert.ToString can handle a null/Nothing argument, and would just return "".
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
If SelectedValue is nothing, Convert.ToString will return an empty string. If the object itself is nothing, you'll still get the error... But I'm assuming this is just a named control, which would be set as soon as the window/form is instantiated.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
I have combo box that is loaded dynamically. When the user select Misc from the list I want to unhide a text field. I recived the following error. System.NullReferenceException: Object reference not set to an instance of an object. at ACEPackaging.dlgEditSkidWeight.HideCarrerTextBox() etc etc etc Here is my code that is called when the user changes the selection on the combobox.
If CStr(cboCarrier.SelectedValue.ToString) = "99" Then
tbCarrierDesc.Visible = True
Else
tbCarrierDesc.Visible = False
End If
End IfI did a little research and said that this error is caused by the .ToString being Null or Nothing. So here is what I have added to correct the problem so there are no more errors. Please let me knwo if this will work.
If Not cboCarrier.SelectedValue.ToString Is Nothing Then 'added this line If CStr(cboCarrier.SelectedValue.ToString) = "99" Then tbCarrierDesc.Visible = True Else tbCarrierDesc.Visible = False End If End If
CStr(cboCarrier.SelectedValue)
Won't throw an exception.
cboCarrier.SelectedValue.ToString
Might throw an exception if SelectedValue is
Nothing
, since you can't call a method on an object if the object isn't there. Hence, we can say;CStr(cboCarrier.SelectedValue.ToString)
Might throw an exception, since the
ToString
will be evaluated prior to theCStr
function.I are Troll :suss: