NullReferenceException

Hi everybody.

I have this problem with a bound checkbox. It is in a vb.net 2000 project that has been converted to 2005. I have bound a checkbox to a column name in a dataset. When testing the app and loading the form which has this chkUpdated control on it, I get a NullReferenceException, object not set to instance of new object...

Binding Code:

Me.chkUpdated.DataBindings.Add(New System.Windows.Forms.Binding("Checked", Me.QuoteHeader1, "Quote_Header.QUOTE_UPDATED"))

Exception String:

ex.ToString "System.NullReferenceException: Object reference not set to an instance of an object.
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.Windows.Forms.Binding.SetPropValue(Object value)
at System.Windows.Forms.Binding.PushData(Boolean force)
at System.Windows.Forms.Binding.UpdateIsBinding()
at System.Windows.Forms.Binding.CheckBinding()
at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.ControlBindingsCollection.Add(Binding binding)
at Quotes.frmQuotes.ApplyBinding()" String

I have checked field names and properties, dataset properties and also checked through my code but cannot understand whats wrong.

If anybody knows where to start with narrowing down the error location then it would be a great help.

Many thanks in advance.

Keyth



Answer this question

NullReferenceException

  • vishuonline

    If you've ensured that Me.QuoteHeader1 has a value set, then check your property name. It might be that the final arguement should just be "QUOTE_UDPATE".



  • Vijay Singh

    I found the cause of this problem.

    I was trying to bind a boolean value to a checkbox and the form had not yet fully initialised. The problem with this is that the checkbox checkstate controlled the enabled property of various other form controls. When the checkbox value changed to true, the program was trying to change the enabled property of various controls causing the NullReferenceException.

    To bypass the problem, I added an 'OnLoad' boolean value to the checkbox_CheckedChanged routine and the exception no longer gets raised.

    Private Sub ApplyBinding()

    OnLoad = True

    .. routine here ..

    OnLoad = False

    End Sub

    Private Sub chkUpdated_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkQuoteUpdated.CheckedChanged

    If OnLoad = False Then

    .. routine here ..

    End If

    End Sub


  • NullReferenceException