locked
Referenced object has a value of 'Nothing' RRS feed

  • Question

  • User1351074292 posted

    Hello,

    I am trying to get the value of a dropdownlist and post it to the viewstate and I am encountering this error:

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 142:                'Mimick the DropDownList
    Line 143:                Dim ddl As DropDownList = CType(ctrl, DropDownList)
    Line 144:                If  Not IsNothing(ddl.SelectedItem.ToString) Then
    Line 145:                    'Removed code for message'
    Line 153: End If

    When I place a breakpoint on that line and add a watch it says the value is "Referenced object has a value of 'Nothing'"

    How can I check for a value of nothing?  I thought that is what the IsNothing was for.  I have tried Convert.ToString() and still the same result.

    Thank you for your help.

    MBG

    Wednesday, October 14, 2009 4:19 PM

Answers

  • User955742345 posted

    It's telling you that either ddl or the SelectedItem property is nothing (most likely the property unless you created a dynamic ddl).

    Basically, see if an item is selected before you check to see what's in it:

     

    Dim ddl As DropDownList = CType(ctrl, DropDownList)
    If  Not IsNothing(ddl.SelectedItem) Then
     '    
    End If
    
    ' or, you could look at the selectedIndex; -1 means no selection. >=0 means selection.
    If ddl.SelectedIndex > -1 Then
     ''
    End If


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2009 5:28 PM

All replies

  • User955742345 posted

    It's telling you that either ddl or the SelectedItem property is nothing (most likely the property unless you created a dynamic ddl).

    Basically, see if an item is selected before you check to see what's in it:

     

    Dim ddl As DropDownList = CType(ctrl, DropDownList)
    If  Not IsNothing(ddl.SelectedItem) Then
     '    
    End If
    
    ' or, you could look at the selectedIndex; -1 means no selection. >=0 means selection.
    If ddl.SelectedIndex > -1 Then
     ''
    End If


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2009 5:28 PM
  • User1351074292 posted

    Thank you for the suggestion.  I was looking for a way to avoid adding another If statement but it seems like the only alternative.  Thank you.

     

    MBG

    Thursday, October 15, 2009 12:41 PM
  • User955742345 posted

    If it helps, you still only need one If statement.  If an item is selected, the value of its name and value properties will NEVER be nothing.  They will be at least an empty string. 

    Thursday, October 15, 2009 1:07 PM
  • User1351074292 posted

    Thank you.  You would be correct that only 1 statement is needed.  I have other checks/procedures running and will need to add another IF statement to get those results prior to processing the page.  The extra If is just for my unique situation.  Thank you everyone for your suggestions and help. 

    Monday, October 19, 2009 12:20 PM