+ Reply to Thread
Results 1 to 5 of 5

Thread: VB Cycling through an array of checkboxes

  1. #1
    simonthecat's Avatar
    simonthecat is offline x10Hosting Member simonthecat is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    56

    VB Cycling through an array of checkboxes

    I am working on a final project for college. I am trying to put the selectedTopping checkboxes into an array so that I can use them in my for statement. I must not be putting the checkboxes into the array right though, because I get the "Object reference not set to an instance of an object." error.

    Thanks for your help.

    Code:
    Dim AllCheckboxes As CheckBox() = {selectedTopping0, selectedTopping1, selectedTopping2, selectedTopping3, selectedTopping4, selectedTopping5}
    Code:
    For i As Integer = 0 To 5
                If (currentSelectedToppings(toppingIndex, i) = "True") Then
                    AllCheckboxes(i).Checked = True
                Else
                    AllCheckboxes(i).Checked = False
                End If
            Next i

    Code:
    System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=Food_ordering
      StackTrace:
           at Food_ordering.pizzaTopping.setCheckBoxes(String topping) in \\gc-ironcase\mydocs\#####\Documents\Visual Studio 2010\Projects\Food_ordering\pizzaTopping.vb:line 56
           at Food_ordering.pizzaTopping.displayToppings(String toppingType) in \\gc-ironcase\mydocs\#####\Documents\Visual Studio 2010\Projects\Food_ordering\pizzaTopping.vb:line 20
           at Food_ordering.pizzaCustom.loadTopping(String toppingType) in \\gc-ironcase\mydocs\#####\Documents\Visual Studio 2010\Projects\Food_ordering\pizzaCustom.vb:line 13
           at Food_ordering.pizzaCustom.vegetablesCheckBox_Click(Object sender, EventArgs e) in \\gc-ironcase\mydocs\#####\Documents\Visual Studio 2010\Projects\Food_ordering\pizzaCustom.vb:line 48
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.CheckBox.OnClick(EventArgs e)
           at System.Windows.Forms.CheckBox.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at Food_ordering.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    Last edited by simonthecat; 12-07-2010 at 10:38 AM.

  2. #2
    Submariner is offline x10Hosting Member Submariner is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    TN, USA
    Posts
    44

    Re: VB Cycling through an array of checkboxes

    Did you ever initialize the individual checkboxes selectedTopping0 -> 5? If not then attempting to access any property of it will result in a Null Reference Excpetion since they are still null objects...

    Have fun,
    James

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: VB Cycling through an array of checkboxes

    The error is thrown in line 56, but you haven't told us which line that is. What's line 56?

    It's very likely that the selected topping checkboxes are already in a collection of some sort, which you could use instead of creating your own.

    Sample code should be self contained and concise. Your sample strayed a little too far beyond concise.
    Last edited by misson; 12-08-2010 at 01:06 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    simonthecat's Avatar
    simonthecat is offline x10Hosting Member simonthecat is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    56

    Re: VB Cycling through an array of checkboxes

    For i As Integer = 0 To 5
    If (currentSelectedToppings(toppingIndex, i) = "True") Then
    AllCheckboxes(i).Checked = True
    Else
    AllCheckboxes(i).Checked = False
    End If
    Next i
    The error happens at the line in bold above.

    Submariner, I'm not exactly sure what you mean about initializing the individual check boxes. If I where to directly say selectedTopping0.Checked = False I know it would work if that helps you understand where the code is at.

    Pretty much I don't fully understand the concept of putting buttons, checkboxes, and so on into arrays. I have searched online for a code example I could follow, but was unable to find something that did what I want.

    ---------- Post added at 11:16 AM ---------- Previous post was at 10:27 AM ----------

    Hey thank you all for taking a look at my problem. My teacher found a code example somewhere and figured out how to do it. Here is the code I used so that anyone else searching will find it.
    Code:
    Private AllCheckboxes(5) As CheckBox
    Code:
    AllCheckboxes(0) = selectedTopping0
            AllCheckboxes(1) = selectedTopping1
            AllCheckboxes(2) = selectedTopping2
            AllCheckboxes(3) = selectedTopping3
            AllCheckboxes(4) = selectedTopping4
            AllCheckboxes(5) = selectedTopping5
    
            For i As Integer = 0 To 5
                If (currentSelectedToppings(toppingIndex, i) = "True") Then
                    AllCheckboxes(i).Checked = True
                Else
                    AllCheckboxes(i).Checked = False
                End If
            Next i
    Simple once you know how to do it.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: VB Cycling through an array of checkboxes

    Shouldn't the array AllCheckboxes be declared with size 6, not 5? You've got six checkboxes, after all.

    Filling the array by assigning from the individual checkboxes couples the form definition and setCheckBoxes too tightly. If you add or remove a checkbox, you'll need to edit setCheckBoxes, which makes the implementation brittle. It might work well enough in homework, but in a large team project where not everyone is familiar with the entire code base, the coupling will cause problems, even if it's documented. You shouldn't have to create your own collection of checkboxes; one should already exist. For example, if you have a reference to the form, it has a Controls collection containing all the input elements in the form, which you can iterate over, looking for checkboxes. You could use this to construct an array of checkboxes, or have setCheckBoxes iterate over the Controls array.
    Last edited by misson; 12-08-2010 at 09:48 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. PHP array help
    By mash99 in forum Programming Help
    Replies: 1
    Last Post: 04-25-2010, 04:22 PM
  2. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  3. Checkboxes and AJAX
    By playminigames in forum Programming Help
    Replies: 4
    Last Post: 10-28-2009, 05:09 PM
  4. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers