VB Cycling through an array of checkboxes

simonthecat

Member
Messages
60
Reaction score
0
Points
6
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:

Submariner

New Member
Messages
44
Reaction score
1
Points
0
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
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:

simonthecat

Member
Messages
60
Reaction score
0
Points
6
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. :biggrin:
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
Top