Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET System.NullReferenceException [duplicate]

I have a VB.NET program that builds multiple datasets from 10 different databases.

I am getting this Exception:

System.NullReferenceException: Object referenced not set to an 
instance of an object:

This error happens on the following lines:

Me.OverTableAdapter.Adapter.SelectCommand.CommandTimeout = 60000
Me.OverTableAdapter.Fill(Me.Dataset.Over, TodayDt, TodayEnd)

What does this Exception mean?

like image 599
Shmewnix Avatar asked Mar 03 '26 03:03

Shmewnix


2 Answers

Well if it's failing on this line:

Me.OverTableAdapter.Adapter.SelectCommand.CommandTimeout = 60000

Then either:

  • Me.OverTableAdapter is Nothing
  • Me.OverTableAdapter.Adapter is Nothing
  • Me.OverTableAdapter.SelectCommand is Nothing

(The second line you've shown us is irrelevant, as you're not getting that far.)

We can't tell based on what you've shown us, but you should be able to find out either in a debugger or by adding diagnostic logging.

Once you've worked out why it's failing, fixing it should be easy - it's almost certainly just a matter of initializing it properly. Compare your initialization of this adapter with the initialization of the other ones.

like image 51
Jon Skeet Avatar answered Mar 04 '26 15:03

Jon Skeet


The reason for this exception is that the SelectCommand will be inititalized from TableAdapter.Fill and not before. So when you try to change the Timeout before you get the NullReferenceException.

You can extend the TableAdapter by creating the partial class in a separate file than the designer.vb/designer.cs.

Namespace DataSet1TableAdapters

    Partial Public Class OverTableAdapter
        Public Property CommandTimeout As System.Int32
            Get
                If Me.CommandCollection Is Nothing OrElse Me.CommandCollection.Length = 0 Then
                    Return -1
                Else
                    Return Me.CommandCollection(0).CommandTimeout
                End If
            End Get
            Set(value As System.Int32)
                If Not Me.CommandCollection Is Nothing Then
                    For Each cmd In Me.CommandCollection
                        cmd.CommandTimeout = value
                    Next
                End If
            End Set
        End Property
    End Class

End Namespace

Now you can use this property instead after you've created an instance of the adapter.

like image 28
Tim Schmelter Avatar answered Mar 04 '26 15:03

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!