Results 1 to 11 of 11

Thread: [RESOLVED] System.NullReferenceException Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    12

    Resolved [RESOLVED] System.NullReferenceException Error

    Hello,
    I am getting NullReferenceException error in the Controls("ComboBox" & m).Text = "No" and probably i didn't define ComboBox and i don't know how to do it. Can you please help?

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim m As Integer
    
            For m = 35 To 77
    
                Controls("ComboBox" & m).Text = "No"
    
            Next m
    
    End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: System.NullReferenceException Error

    Indexing the Controls collection with a name that doesn't exist will return Nothing, and you can't get a property of Nothing. Why do you think that ComboBoxes will just appear out of thin air? If you want ComboBoxes on your form then add them to your form. That said, if you added them to some other container, e.g. a Panel, then they will be in the Controls collection of that container, not of the form.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    12

    Re: System.NullReferenceException Error

    Quote Originally Posted by jmcilhinney View Post
    Indexing the Controls collection with a name that doesn't exist will return Nothing, and you can't get a property of Nothing. Why do you think that ComboBoxes will just appear out of thin air? If you want ComboBoxes on your form then add them to your form. That said, if you added them to some other container, e.g. a Panel, then they will be in the Controls collection of that container, not of the form.
    Hello jmcilhinney,
    You are like The Dooo of programing I added the ComboBoxes in TabControl1, does this make a difference? If so how can i put it to my code?

  4. #4
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: System.NullReferenceException Error

    Code:
     TabControl1.TabPages(x).Controls("ComboBox" & m).text
    where x is the index of the tabpage
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: System.NullReferenceException Error

    Quote Originally Posted by Delaney View Post
    Code:
     TabControl1.TabPages(x).Controls("ComboBox" & m).text
    where x is the index of the tabpage
    If the TabPage was added to the TabControl in the designer then there will be a field for it, just like other controls. In that case, you would generally refer to the TabPage by that field, e.g.
    Code:
    TabPage1.Controls("ComboBox" & m).Text = "No"

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    12

    Re: System.NullReferenceException Error

    Thank you so much guys, solved the problem

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    12

    Re: System.NullReferenceException Error

    I have one more question. When i try to use the same method with SelectedIndex = -1, it's saying "SelectedIndex is not a member of Control". How can i combine it with the Control method?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: System.NullReferenceException Error

    Indexing the Controls collection returns a Control reference, so you can only access members of the Control class via that reference. As is always the case, if the actual object referred to is a more specific type and you want to access members of that type then you need to cast the reference you have as that type. The analogy I always use is a vet who knows that all their patients are animals but not what specific type of animal they will be, so can only do things that apply to all animals until the owner takes their pet out of the box and reveals the specific type of animal it is. If you don't know how to cast in VB then that's what you should research.

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    12

    Re: System.NullReferenceException Error

    Solved it, thanks again

    Code:
    TabPage1.Controls("ComboBox" & m).Text = Nothing
    Last edited by Textiler; Jan 21st, 2021 at 02:52 AM.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: System.NullReferenceException Error

    The other option would be:
    Code:
    DirectCast(TabPage1.Controls("ComboBox" & m),ComboBox).SelectedIndex = -1
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    12

    Re: System.NullReferenceException Error

    Quote Originally Posted by Shaggy Hiker View Post
    The other option would be:
    Code:
    DirectCast(TabPage1.Controls("ComboBox" & m),ComboBox).SelectedIndex = -1
    Thank you, working great

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width