Error: Value cannot be null.

deadscenekid

New member
Joined
Dec 27, 2007
Messages
2
Programming Experience
1-3
I am creating an application that obtains system information. In my program, each different part of my program is contained under tabs in a TabControl and I have it to where there are options to hide and show the tabs with removing and reinserting the tabs and I also have the value whether it be true or false saved into the Application Settings using VS2008 and .NET 3.5 but when I select more three or four checkboxes to hide the tabs I get this error on the next startup:
VB.NET:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
[COLOR="Red"][B]System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at System.Windows.Forms.TabControl.TabPageCollection.Remove(TabPage value)[/B][/COLOR]
   at sys_info.options.CheckBox2_CheckedChanged(Object sender, EventArgs e)
   at System.Windows.Forms.CheckBox.OnCheckedChanged(EventArgs e)
   at System.Windows.Forms.CheckBox.set_CheckState(CheckState value)
   at System.Windows.Forms.CheckBox.set_Checked(Boolean value)
   at sys_info.Form1.Form1_Load(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
sysinfo
    Assembly Version: 1.1.8.0
    Win32 Version: 1.1.8.0
    CodeBase: file:///C:/Program%20Files/Hyperhack%20Software/Hyperhack%20System%20Info/sysinfo.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Any Help?
 
Your code is faulty because it is passing a null reference to the Remove method. Show us the code inside the CheckChanged event handler. I bet you're using a For loop and counting up. The problem is likely to be that as soon as you remove one item from a collection the indexes of all the others change. For instance, if you have two items in a collection you cannot remove the item at index 0 and then the item at index 1 because as soon as you remove the item at index 0 the other item moves down and becomes the item at index 0, so there is no item at index 1. In cases where you're removing items from a collection in a loop you should usually do one of two things:

1. Use a For loop and count backwards, so removing an item doesn't affect the indexes of the items still to be removed.

2. Use a Do or While loop and always remove the first item. That way you just keep removing the first item as long as there is a first item.
 
This is the code inside of the CheckChanged event.
VB.NET:
        If CheckBox2.Checked = True Then
            Form1.TabControl2.TabPages.Remove(Tab2)
        End If
        If CheckBox2.Checked = False Then
            Form1.TabControl2.TabPages.Insert(0, Tab2)
        End If
 
Not what I expected, which is exactly why you should be posting the relevant code in the first place. Basically your Tab2 variable is Nothing. I can't tell you why exactly because I haven't seen the rest of your code. Either you haven't assigned a value to it or you've assigned Nothing to it at some point.

Also, you should never be testing the same value in two separate If statements like that. Let's say that CheckBox2 is checked. You go through the first block and do your thing, then you go and test whether CheckBox2 is not checked. What's the point of that? You already know that it is. In situations like that you should be using an Else block:
VB.NET:
If Me.CheckBox2.Checked Then
    'The CheckBox is checked.
Else
    'The CheckBox is not checked.
End If
 
Back
Top