i got this problem while trying to compile this code
Public Class Form2
Dim db_classobj As Object
Dim textboxobj() As TextBox = {TextBox1, TextBox2}
Dim datagridobj() As DataGridView = {DataGridView1}
Dim temp As New db_class(textboxobj, datagridobj, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=datamhs.accdb", "mhs", "ksmhs")
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
db_classobj = temp
End Sub
End Class
while its compiling, i got this error message :
An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
whats wrong with my code? can someone fix it? i really appreciate it.
thanks.
Change your form load function to this, and remove the
textboxobj,datagridobj, andtempdeclarations from the top of the class.Basically what was happening was that because your
Dim temp as Newstatement was right in the class, it was being executed before the forms constructor, and the constructor is where all your controls (like TextBox1) get assigned their values, so basically you were passing an array ofNothingvalues to yourdb_classconstructor. When you hit this line,_textbox(i).DataBindings.Add("text", ds.Tables(_mailboxname), temp(i)), the_textbox(i)part returnsNothingso the attempted reference to theDataBindingsproperty fails, because it doesn’t exist.