dcsimg
Results 1 to 11 of 11

Thread: Radio button list problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    31

    Question Radio button list problem

    Hey ! Whats wrong with this code? It gives me this error: Additional information: Object reference not set to an instance of an object. It gives me the error at this line:
    Code:
    rb(1) = New RadioButton()
    Code:
    Dim rb As List(Of RadioButton) = Nothing
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            rb(1) = New RadioButton()
            rb(2) = New RadioButton()
            rb(3) = New RadioButton()
            rb(4) = New RadioButton()
            rb(1).Location = vb1.Location
            rb(2).Location = vb2.Location
            rb(3).Location = vb3.Location
            rb(4).Location = vb4.Location
            For i As Integer = 1 To 4
                My.Forms.Form1.Controls.Add(rb(i))
            Next
    End Sub

    Thank you very much guys, I need this for a projekt to school

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,917

    Re: Radio button list problem

    You explicitly set the List to Nothing, so there is no list, yet you are trying to add items to it. If you were thinking that setting the list to Nothing would make an empty list, it does not.

    What you need is this:
    Code:
    Private rb As New List(of RadioButton)
    The rb variable doesn't hold the list, it holds a reference to the list. Effectively, it holds the memory address where the list is located. By setting that to Nothing, you cleared the address, so it held nothing at all. By calling New List(), I am creating the list, which is somewhere out in memory, and rb will now hold that address.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    31

    Re: Radio button list problem

    I replaced the Dim to what you said, and it gives me this error: "Additional information: Index was out of range. Must be non-negative and less than the size of the collection." at this line
    Code:
    rb(1).Location = vb1.Location
    Thank you!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    31

    Re: Radio button list problem

    ....

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    31

    Re: Radio button list problem

    Okaay, I understood that the Nothing is something that clears the address, so it doesnt exist anymore, thanks

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,917

    Re: Radio button list problem

    Yeah, actually, I didn't get on to that part of the code. What you wrote is how you add items to an array, and would kind of work for a list, but isn't quite right. What you want is something like this:

    rb.Add(New RadioButton)

    Essentially, you add items to a list by calling the .Add method (or Insert, if you don't want to keep adding to the end of the list).

    However, you only have four radiobuttons, so maybe an array would simply be more appropriate. Lists are great because you can add, remove, insert, and so on with them. They change size efficiently, whereas arrays do not. So, a List is the thing to use if you don't know how many you will end up adding, but if you know the number then you could just create an array:

    Private rb(3) As RadioButton

    Then your code would work the way you are using it. Of course, if you ever want to change the size at runtime, then a list would still be better.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    31

    Re: Radio button list problem

    Ohh, thank you very very much! You've helped me with a lot questions now I have a one more que. Whats the difference between Dim rb as List(of RadioButton), and Private rb as new List(Of RadioButtons)? So why did you told me to use Private instead of Dim?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,917

    Re: Radio button list problem

    Quote Originally Posted by Daveeed View Post
    Okaay, I understood that the Nothing is something that clears the address, so it doesnt exist anymore, thanks
    Actually, setting to Nothing cleared the address from the variable. The object may still exist, and likely will, but you don't care. That's one of the features. Recovering memory happens only when it needs to happen, cause it can be a bit slow. So, once no variable holds the address of the object, it can linger in memory for quite a long time. If your memory needs aren't too great, it can linger until you close the application. If the application needs more memory, it will go through and clean up all the abandoned objects and recover their memory, but it won't do that unless it has to, cause why bother. This can surprise people, as they may see memory keep increasing. It is cheaper for the application to ask for more memory than to recover wasted memory that it already has, so it will ask for more memory first. As long as the OS has memory to spare, it will meet those requests by giving the application more memory. This can make it look like you have a memory leak, because memory may just keep increasing over time, but what's really happening is that the application is being more efficient. If the OS says no, only then will the application recover the memory.

    By setting the variable to Nothing, you are just saying that that particular variable no longer holds the address of the object. If NO variable holds the address, then the objects is abandoned and will be cleaned up if the application ever decides it has to recover memory. So, it's pretty harmless and inconsequential. There are some rare cases where it is a good thing to do, but generally not.
    My usual boring signature: Nothing

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,917

    Re: Radio button list problem

    Quote Originally Posted by Daveeed View Post
    Ohh, thank you very very much! You've helped me with a lot questions now I have a one more que. Whats the difference between Dim rb as List(of RadioButton), and Private rb as new List(Of RadioButtons)? So why did you told me to use Private instead of Dim?
    In this case, there is no difference at all that I can point to. Some people have suggested that Dim could be treated as either Private or Public, depending on the situation, but I don't know whether or not that's true.

    As a general rule, Dim is used for local variables (you don't get a choice), and either Public, Private, or Protected (that last one is quite rare) are used for variables outside of methods. You don't HAVE to do that, but if Dim can mean different things in different places, then Public and Private are just more explicit. There certainly isn't any harm to using Dim. I have an application that did that for all form level variables (the code was generated, not written), and I've never found any negative consequence from that.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2018
    Posts
    31

    Re: Radio button list problem

    I've replaced the list to this
    Code:
        Private rb(4) As RadioButton
    , but it gives me again the same error,
    Code:
    Additional information: Object reference not set to an instance of an object.
    , at the line
    Code:
    rb(1).Location = vb1.Location
    . Now, everythings should be okay, whats the problem again?

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,917

    Re: Radio button list problem

    What's the whole code now?

    That exception is common, and the solution is always (roughly) the same. The first step is to find the object that is Nothing. In the line shown, there are a couple candidates:

    1) vb1
    2) rb
    3) rb(1)

    If any of those are Nothing, you'd get that exception. However, they aren't all equally likely. I don't know what vb1 is, so I suppose it could be Nothing. You declared an array of radiobuttons, but did you create radiobuttons and put them in there? You did in the original code with these lines:
    Code:
    rb(1) = New RadioButton()
    rb(2) = New RadioButton()
    rb(3) = New RadioButton()
    rb(4) = New RadioButton()
    If those lines are still there, then that would rule out both #2 and #3, because you are creating RadioButtons and putting them into slots in the array (though all arrays start at 0 in .NET, so you have an rb(0), though you aren't using it). However, if those lines are not still there, then you have the array, but it's like an empty egg carton: You have the places to put the eggs, but no eggs in those places.
    My usual boring signature: Nothing

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
  •  



Featured


Click Here to Expand Forum to Full Width