Good day, I would ask for your help, my code below is giving me an error whether I open my new window(application/form?) resulting to appearance of the error "Object reference...." first before the said application. What is the problem in this codes? Thank you for your help. =)

    Private Sub searchacquisition_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchacquisition.TextChanged
        Try
            Connect()
            Dim SQL1 As String
            SQL1 = "Select * from newacquisition"
            FillData(SQL1)
            OleA.Fill(Dset, "newacquisition")
            Dim col2 As New AutoCompleteStringCollection
            Dim c As New Integer
            For c = 0 To Dset.Tables("newacquistion").Rows.Count - 1
                col2.Add(Dset.Tables("newacquisition").Rows(c)("title").ToString())

            Next

            searchacquisition.AutoCompleteSource = AutoCompleteSource.CustomSource
            searchacquisition.AutoCompleteCustomSource = col2
            searchacquisition.AutoCompleteMode = AutoCompleteMode.Suggest
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

`

Dani AI

Generated

Quick diagnosis and likely causes: a NullReferenceException happens when code tries to use an object reference that is Nothing. In this thread the usual suspects are the DataSet (Dset), the data adapter (OleA) or a mismatched table name. The posted handler runs on TextChanged (which can fire during form construction), so the handler may execute before any initialization has completed. That echoes ’s and ’s suggestions but narrows it down: a misspelled table key (the table name used when filling vs. the name used when reading) will make Dset.Tables(...) return Nothing and accessing .Rows will throw the exception.

Quick checks and a defensive pattern:

  • Run with the debugger and break on all exceptions; inspect Dset, Dset.Tables.Count, Dset.Tables.Contains("newacquisition") and OleA before the loop.
  • Log ex.Message and ex.StackTrace from the Catch to get the failing line.
  • Use guards before iterating so the code fails gracefully:
If Dset Is Nothing OrElse Not Dset.Tables.Contains("newacquisition") Then
    ' no data — skip population or log an error
Else
    For Each r As DataRow In Dset.Tables("newacquisition").Rows
        col2.Add(Convert.ToString(r("title")))
    Next
End If

Recommended fixes and best practices: initialize Dset (for example at declaration), create and fill the adapter locally (or ensure OleA is constructed before the event can run), and populate AutoComplete only after the fill completes (Form.Shown or after a dedicated PopulateAutoComplete method). Avoid heavy work in TextChanged — either populate once at load or guard the handler with an "isReady" flag so it ignores early firings. Also verify that the table name used when filling exactly matches the name used when reading.

Recommended Answers

All 2 Replies

It would help to know which line was referenced in the error.

If I'm not mistaken, generally that error is from not initializing an object when it is declared, then trying to give it a value. In your code SQL1, Dset, searchacquisition, OleA might fall in that category. SQL1 is simple, try this, Dim SQL1 As String = "Select * from newacquisition". It looks like searchacquisition is a textbox, so it should be OK. That leaves Dset and OleA to check in you're other code.

Hi!

The error you are receiving, appears when for example we do not initialize an object and try to access one of its property so in this case the the object does not exists so we can not access its property.

I have a couple of questions and few guidelines for you:

Questions:

1) Have you tried to step debug it?
2) Have you checked which line is the cause of this error ?

Guidelines:

1) Verify that, is 'OleA' variable initialized ?
2) In these lines:

FillData(SQL1)
OleA.Fill(Dset, "newacquisition")

what is your intentions here ? Is it a TWO times fill ? 'OleA.Fill' is already filling 'Dset' with data. Isn't it ?

3) Make sure this at this line :

col2.Add(Dset.Tables("newacquisition").Rows(c)("title").ToString())

we have a table inside which contains atleast one table with name "newacquisition" and a column in it with name "title".

4) Is search 'searchacquisition' variable initialized ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.