error code

tirso

Active member
Joined
Nov 15, 2006
Messages
31
Programming Experience
1-3
Hi! to all, does anyone could help me how to find the error of the code below. The error message is "An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."

VB.NET:
    Private strConnection As String = _
        "Provider = Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source =" & txtDirectory.Text & ";"

Tirso
 
You cannot instantiate a string in this way. In the class, you can instantiate it with

VB.NET:
Private strConnection As String = String.Empty

and in the form load event, you put in

VB.NET:
strConnection = _
        "Provider = Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source =" & txtDirectory.Text & ";"

The above code will serve a purpose if you put the value for txtDirectory in design time. Otherwise, I don't think putting the above code in the form.load event is a good idea.
 
Of course you can initialize a string variable at class level. The problem with your string initializing is that it accesses a 'txtDirectory' control that don't exist until after the form in constructed. This means you cannot access the control before the forms Sub New contructor have finished its InitializeComponent stage where the controls on the form is initialized. Form.Load event happens after the form instance is created and can therefore be used to access this control property as lingsn suggested.
 
Back
Top