你调用的对象是空的
我有一个具有搜索功能的表单 - 单个文本字段和命令按钮;填写文本字段后,将执行数据库查询,并且结果(如果返回一个结果)通过动态控制字段显示在表单上。
第一次使用搜索功能时,会创建字段并从数据库返回数据,但是重新运行搜索功能时,我收到错误“对象引用未设置为对象的实例” ,错误发生在:
initSearch(txtSearchInput.Text)
我猜测我没有正确处理此类使用的文本字段,任何人都可以建议我还应该怎么做?
txtSearchInput 不是动态字段,它是通过设计模式创建的,命令按钮也是如此。上面的代码位于命令按钮单击事件中:
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
任何帮助将不胜感激。
谢谢,
马特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误不在您发布的代码中。 TextBox 的 Text 属性以及对 Form Textbox 的引用不会突然变为 null。
您可能必须调试到 initSearch
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
您是否在调试器中测试 txtSearchInput 是否为空?
异常可能是从 initSearch 函数中冒出来的,最好的方法是调试代码。
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.