-1

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.

Yohanim
  • 3,319
  • 9
  • 52
  • 92
  • I'd say one of your object references is not set to an instance of an object. This normally means you are accessing a property on an object that is `Nothing`. The exception will occur a a specific line of your program, which is it? What is `db_class` and what happens in the five parameter constructor you are calling? – Jodrell Dec 06 '12 at 16:12
  • I am no specialist of vb.net, but I think your problems comes from the fact that the controls TextBox1, TextBox2 and DataGridView1 are not yet created. I would try to move their assignemnts in the form2_load. – Skyp Dec 06 '12 at 16:12
  • i don't know what should i do right now, can you explain step by step? – Yohanim Dec 06 '12 at 16:22
  • which line in db_class constructor throws the exception? – Jodrell Dec 06 '12 at 16:26
  • Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 06 '12 at 16:32
  • but, which line throws the exception? – Jodrell Dec 06 '12 at 16:38

3 Answers3

1

The parameters you are passing to the constructor of db_class are of the correct type but not the correct value. They are causing the constructor of db_class to throw this exception.

Without knowing which line of the db_class constructor is throwing the exception we can not help you.

Is it this line?

_textbox(i).DataBindings.Add("text", ds.Tables(_mailboxname), temp(i))
Jodrell
  • 34,946
  • 5
  • 87
  • 124
1

Change your form load function to this, and remove the textboxobj, datagridobj, and temp declarations from the top of the class.

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     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")
     db_classobj = temp
End Sub

Basically what was happening was that because your Dim temp as New statement 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 of Nothing values to your db_class constructor. When you hit this line, _textbox(i).DataBindings.Add("text", ds.Tables(_mailboxname), temp(i)), the _textbox(i) part returns Nothing so the attempted reference to the DataBindings property fails, because it doesn't exist.

Kratz
  • 4,280
  • 3
  • 32
  • 55
0

You must create array first.

Dim textboxobj() As TextBox = New TextBox(){TextBox1, TextBox2}
Dim datagridobj() As DataGridView = New DataGridView(){DataGridView1}
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
  • its still appear, An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object. – Yohanim Dec 06 '12 at 16:17
  • This is not actually true and, if it were, the code above would not compile. – Jodrell Dec 06 '12 at 16:20