Lima
Lima

Reputation: 1211

Object Reference not set to an instance of an object

I have a form which has a search feature - a single text field and command button; when the text field is filled-in a database query is executed and the result (if one result returned) is shown on the form via dynamic control fields.

When the search feature is used for the first time, the fields are created and the data is returned from the database, however when the search feature is re-ran I am getting the error "Object Reference not set to an instance of an object", the error occurs at:

initSearch(txtSearchInput.Text)

I am guessing that I am not handling the textfield properly for this type of use, can anyone please advise how else I should be doing this?

The txtSearchInput is not a dynamic field, it has been created through the design mode, the same for the command button. The above code is located in the command button On Click event:

 Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Try
            initSearch(txtSearchInput.Text)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Encountered")
        End Try


    End Sub

Any help would be greatly appreciated.

Thanks,

Matt

Upvotes: 0

Views: 420

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273824

Any help would be greatly appreciated.

The error is not in the code you posted. The Text property of a TextBox, and the reference to a Form Textbox don't become null all of a sudden.

You probably have to debug into initSearch

Upvotes: 3

Sandeep Singh Rawat
Sandeep Singh Rawat

Reputation: 1647

Did you test in the debugger if txtSearchInput is null?

Exception could be bubbling up from initSearch function, best way is to debug your code.

Upvotes: 0

Related Questions