"Value cannot be null" Weirdness

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
I'm having an issue when trying to setup a data source for a set of combo boxes. I've found success, but only after working around an error stating that "value cannot be null"

It took my a while to find the source of this being that the breakpoint after the exception was placed a few lines after what I found to actually be causing the error.

I have this class
Public Class FailureFixTab : Inherits ActionTab
    Sub New(ByRef ParentForm As Object)
        MyBase.New("FailureFix", ParentForm)
    End Sub
    Protected Overrides Sub InitializeControls()
        Try
            Dim cbSelectedFailure As New ComboBox With {.Parent = Me.ChildContainerControl, .SelectedItem = Nothing}
            Dim cbSelectedFix As New ComboBox With {.Parent = Me.ChildContainerControl, .SelectedItem = Nothing}
            Dim btnSaveFailureFix As New Button With {.Name = "btnSaveFailureFix", .Parent = Me.ChildContainerControl, .Text = "Save"}
        Catch ex As Exception


        End Try


    End Sub
    Protected Overrides Sub InitializeEventHandlers()
        Try
            AddHandler ChildContainerControl.Controls.Find("btnSaveFailureFix", True)(0).Click, AddressOf Me.ActionEvent
        Catch ex As Exception


        End Try


    End Sub
    Protected Overrides Sub ActionEvent(sender As Object, e As EventArgs)
        If IsNothing(ParentFormReference.RepairInformation) Then
            MsgBox("RepairInformation is empty!")
        Else
            Dim rc As New RepairControl
            rc.SetStatusAndAssignment(ParentFormReference.RepairInformation.SerialNumber, ParentFormReference.User, ParentFormReference.RepairInformation.Status)
        End If
    End Sub
End Class




This class works really great with one exception, when I try to set the datasource in "initialize controls" (which is called in the Mybase.New(string, object) constructor) I get errors and other weirdness (like invisible controls that still have events, I.E -> I can hit the drop down arrow of the combo box but nothing appears).

As a workaround I have set the datasources outside of this class like so:

        Dim addcommentstab As New CommentTab(Me) With {.Text = "Add Comments"}
        Dim AddFailureFixTab As New FailureFixTab(Me) With {.Text = "Enter Failure/Fix"}
        TabControl1.TabPages.Add(addcommentstab)
        TabControl1.TabPages.Add(AddFailureFixTab)
        Dim rc As New RepairControl


        DirectCast(AddFailureFixTab.ChildContainerControl.Controls(0), ComboBox).DataSource = rc.GetFailureFix(1)
        DirectCast(AddFailureFixTab.ChildContainerControl.Controls(1), ComboBox).DataSource = rc.GetFailureFix(2)
        DirectCast(AddFailureFixTab.ChildContainerControl.Controls(1), ComboBox).SelectedItem = Nothing


This works just fine.

Again the error only occurs if I try to set the datasource within the FailureFixTab object, "value cannot be null". The breakpoint ends up getting set at the next step after the constructor these controls are initialized in.

Any ideas what could be broken here?
 
Did you look at the stack trace for the exception? That will tell you exactly which method the exception is being thrown in and that will tell you which value cannot be null. You should also look at the type of the exception. I'm guessing that it's an ArgumentNullException, which means that Nothing is being passed an argument to a method that is not allowed to be Nothing. Once you know which method and which parameter, then you can trace the source of the null value.
 
stack trace, let me do that, because there is no "details" when the exception is thrown, i thought i was stuck in the dark..

And yes, it's an argumentnullexception...

The weird thing is that it's not showing a stack trace when the exception is thrown.
Normally I can click details and it's much more specific.

I'll give it a look tomorrow, I have to wrap up final testers and shippers and finally go home for the day D:
 
Last edited:
This issue was due to the fact that I had a null entry in the database and the combobox apparently did not like that.

    Protected Overrides Sub InitializeControls()
        Dim rc As New RepairControl
        Try
            Dim cbSelectedFailure As New ComboBox With {.Parent = Me.ChildContainerControl, .DataSource = rc.GetFailureFix(1), .SelectedItem = Nothing}
            Dim cbSelectedFix As New ComboBox With {.Parent = Me.ChildContainerControl, .DataSource = rc.GetFailureFix(2), .SelectedItem = Nothing}
            Dim btnSaveFailureFix As New Button With {.Name = "btnSaveFailureFix", .Parent = Me.ChildContainerControl, .Text = "Save"}
        Catch ex As Exception


        End Try


    End Sub

This works fine
 
Back
Top