Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. System.NullReferenceException: Check my solution someone, pls

System.NullReferenceException: Check my solution someone, pls

Scheduled Pinned Locked Moved Visual Basic
help
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KCDude
    wrote on last edited by
    #1

    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 If

    I 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 L L 3 Replies Last reply
    0
    • K KCDude

      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 If

      I 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 Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      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 If

      As 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)

      K 1 Reply Last reply
      0
      • K KCDude

        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 If

        I 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
        
        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • I Ian Shlasko

          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 If

          As 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)

          K Offline
          K Offline
          KCDude
          wrote on last edited by
          #4

          What if the object hasn't been set?

          I 1 Reply Last reply
          0
          • K KCDude

            What if the object hasn't been set?

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            0
            • K KCDude

              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 If

              I 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
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 the CStr function.

              I are Troll :suss:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups